APIdia Launch Partner
Top Description Fields Constructors Methods
com.badlogic.gdx.utils

public Class ByteArray

extends Object
Class Inheritance
Imports
com.badlogic.gdx.math.MathUtils, java.util.Arrays

A resizable, ordered or unordered byte array. Avoids the boxing that occurs with ArrayList. If unordered, this class avoids a memory copy when removing elements (the last element is moved to the removed element's position).
Author
Nathan Sweet

Field Summary

Modifier and TypeField and Description
public byte[]
public boolean
public int

Constructor Summary

AccessConstructor and Description
public
ByteArray()

Creates an ordered array with a capacity of 16.

public
ByteArray(int capacity)

Creates an ordered array with the specified capacity.

public
ByteArray(boolean
If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
ordered
,
int
Any elements added beyond this will cause the backing array to be grown.
capacity
)

public
ByteArray(ByteArray array)

Creates a new array containing the elements in the specific array.

public
ByteArray(byte[] array)

Creates a new ordered array containing the elements in the specified array.

public
ByteArray(boolean
If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
ordered
,
byte[] array, int startIndex, int count)

Creates a new array containing the elements in the specified array.

Method Summary

Modifier and TypeMethod and Description
public void
add(byte value)

public void
add(byte value1, byte value2)

public void
add(byte value1, byte value2, byte value3)

public void
add(byte value1, byte value2, byte value3, byte value4)

public void
public void
addAll(ByteArray array, int offset, int length)

public void
addAll(byte... array)

public void
addAll(byte[] array, int offset, int length)

public void
public boolean
contains(byte value)

public byte[]

Returns:

items
ensureCapacity
(int additionalCapacity)

Increases the size of the backing array to accommodate the specified number of additional items.

public boolean
equals(Object object)

Overrides java.lang.Object.equals.

Returns false if either array is unordered.

public byte
first()

Returns the first item.

public byte
get(int index)

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public void
incr(int index, byte value)

public void
incr(byte value)

public int
indexOf(byte value)

public void
insert(int index, byte value)

public void
insertRange(int index, int count)

Inserts the specified number of items at the specified index.

public boolean
isEmpty()

Returns true if the array is empty.

public int
lastIndexOf(byte value)

public void
mul(int index, byte value)

public void
mul(byte value)

public boolean
notEmpty()

Returns true if the array has one or more items.

public byte
peek()

Returns the last item.

public byte
pop()

Removes and returns the last item.

public byte
random()

Returns a random item from the array, or zero if the array is empty.

public boolean

Returns:

true if this array was modified.
removeAll
(ByteArray array)

Removes from this array the first instance of each element contained in the specified array.

public int
removeIndex(int index)

Removes and returns the item at the specified index.

public void
removeRange(int start, int end)

Removes the items between the specified indices, inclusive.

public boolean
removeValue(byte value)

public int

Returns:

the number of replacements done
replaceAll
(byte
the byte to be replaced
value
,
byte
all occurrences of value will be replaced by this replacement
replacement
)

Returns the number of replacements done.

public boolean

Returns:

if value was found and replaced
replaceFirst
(byte
the byte to be replaced
value
,
byte
the first value will be replaced by this replacement if found
replacement
)

Returns true if the specified value was replaced successfully with the replacement

protected byte[]
resize(int newSize)

public void
public void
set(int index, byte value)

public byte[]

Returns:

items
setSize
(int newSize)

Sets the array size, leaving any values beyond the current size undefined.

public byte[]

Returns:

items
shrink
()

Reduces the size of the backing array to the size of the actual items.

public void
public void
sort()

public void
swap(int first, int second)

public byte[]
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public String
toString(String separator)

public void
truncate(int newSize)

Reduces the size of the array to the specified size.

public static ByteArray
with(byte... array)

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

itemsback to summary
public byte[] items
orderedback to summary
public boolean ordered
sizeback to summary
public int size

Constructor Detail

ByteArrayback to summary
public ByteArray()

Creates an ordered array with a capacity of 16.

ByteArrayback to summary
public ByteArray(int capacity)

Creates an ordered array with the specified capacity.

ByteArrayback to summary
public ByteArray(boolean ordered, int capacity)
Parameters
ordered:boolean

If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.

capacity:int

Any elements added beyond this will cause the backing array to be grown.

ByteArrayback to summary
public ByteArray(ByteArray array)

Creates a new array containing the elements in the specific array. The new array will be ordered if the specific array is ordered. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.

ByteArrayback to summary
public ByteArray(byte[] array)

Creates a new ordered array containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.

ByteArrayback to summary
public ByteArray(boolean ordered, byte[] array, int startIndex, int count)

Creates a new array containing the elements in the specified array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.

Parameters
ordered:boolean

If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.

Method Detail

addback to summary
public void add(byte value)
addback to summary
public void add(byte value1, byte value2)
addback to summary
public void add(byte value1, byte value2, byte value3)
addback to summary
public void add(byte value1, byte value2, byte value3, byte value4)
addAllback to summary
public void addAll(ByteArray array)
addAllback to summary
public void addAll(ByteArray array, int offset, int length)
addAllback to summary
public void addAll(byte... array)
addAllback to summary
public void addAll(byte[] array, int offset, int length)
clearback to summary
public void clear()
containsback to summary
public boolean contains(byte value)
ensureCapacityback to summary
public byte[] ensureCapacity(int additionalCapacity)

Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes.

Returns:byte[]

items

equalsback to summary
public boolean equals(Object object)

Overrides java.lang.Object.equals.

Returns false if either array is unordered.

firstback to summary
public byte first()

Returns the first item.

getback to summary
public byte get(int index)
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
incrback to summary
public void incr(int index, byte value)
incrback to summary
public void incr(byte value)
indexOfback to summary
public int indexOf(byte value)
insertback to summary
public void insert(int index, byte value)
insertRangeback to summary
public void insertRange(int index, int count)

Inserts the specified number of items at the specified index. The new items will have values equal to the values at those indices before the insertion.

isEmptyback to summary
public boolean isEmpty()

Returns true if the array is empty.

lastIndexOfback to summary
public int lastIndexOf(byte value)
mulback to summary
public void mul(int index, byte value)
mulback to summary
public void mul(byte value)
notEmptyback to summary
public boolean notEmpty()

Returns true if the array has one or more items.

peekback to summary
public byte peek()

Returns the last item.

popback to summary
public byte pop()

Removes and returns the last item.

randomback to summary
public byte random()

Returns a random item from the array, or zero if the array is empty.

removeAllback to summary
public boolean removeAll(ByteArray array)

Removes from this array the first instance of each element contained in the specified array.

Returns:boolean

true if this array was modified.

removeIndexback to summary
public int removeIndex(int index)

Removes and returns the item at the specified index.

removeRangeback to summary
public void removeRange(int start, int end)

Removes the items between the specified indices, inclusive.

removeValueback to summary
public boolean removeValue(byte value)
replaceAllback to summary
public int replaceAll(byte value, byte replacement)

Returns the number of replacements done.

Parameters
value:byte

the byte to be replaced

replacement:byte

all occurrences of value will be replaced by this replacement

Returns:int

the number of replacements done

replaceFirstback to summary
public boolean replaceFirst(byte value, byte replacement)

Returns true if the specified value was replaced successfully with the replacement

Parameters
value:byte

the byte to be replaced

replacement:byte

the first value will be replaced by this replacement if found

Returns:boolean

if value was found and replaced

resizeback to summary
protected byte[] resize(int newSize)
reverseback to summary
public void reverse()
setback to summary
public void set(int index, byte value)
setSizeback to summary
public byte[] setSize(int newSize)

Sets the array size, leaving any values beyond the current size undefined.

Returns:byte[]

items

shrinkback to summary
public byte[] shrink()

Reduces the size of the backing array to the size of the actual items. This is useful to release memory when many items have been removed, or if it is known that more items will not be added.

Returns:byte[]

items

shuffleback to summary
public void shuffle()
sortback to summary
public void sort()
swapback to summary
public void swap(int first, int second)
toArrayback to summary
public byte[] toArray()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

toStringback to summary
public String toString(String separator)
truncateback to summary
public void truncate(int newSize)

Reduces the size of the array to the specified size. If the array is already smaller than the specified size, no action is taken.

withback to summary
public static ByteArray with(byte... array)
See Also
ByteArray(byte[])