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

public Class Array<T>

extends Object
implements Iterable<T>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable
Known Direct Subclasses
com.badlogic.gdx.utils.SnapshotArray, com.badlogic.gdx.utils.DelayedRemovalArray
Imports
java.util.Arrays, .Comparator, .Iterator, .NoSuchElementException, com.badlogic.gdx.math.MathUtils, com.badlogic.gdx.utils.reflect.ArrayReflection

A resizable, ordered or unordered array of objects. 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

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
public static class

Field Summary

Modifier and TypeField and Description
public T[]
items

Provides direct access to the underlying array.

private transient Array.ArrayIterable<T>
public boolean
private transient Predicate.PredicateIterable<T>
public int

Constructor Summary

AccessConstructor and Description
public
Array()

Creates an ordered array with a capacity of 16.

public
Array(int capacity)

Creates an ordered array with the specified capacity.

public
Array(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
Array(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
,
ArraySupplier<T[]> arraySupplier)

Creates a new array with items with the specified supplier.

public
Array(ArraySupplier<T[]> arraySupplier)

Creates an ordered array with items with the specified supplier and a capacity of 16.

public
Array(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
,
Class<T> arrayType)

Creates a new array with items of the specified type.

public
Array(Class<T> arrayType)
Deprecated Use Array#Array(ArraySupplier) instead

Creates an ordered array with items of the specified type and a capacity of 16.

public
Array(Array<? extends T> array)

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

public
Array(T[] array)

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

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

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

Method Summary

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

public void
add(T value1, T value2)

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

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

public void
addAll(Array<? extends T> array)

public void
addAll(Array<? extends T> array, int start, int count)

public void
addAll(T... array)

public void
addAll(T[] array, int start, int count)

public void
public boolean
contains(T
May be null.
value
,
boolean
If true, == comparison will be used. If false, .equals() comparison will be used.
identity
)

Returns true if this array contains the specified value.

public boolean
containsAll(Array<? extends T>
May contains nulls.
values
,
boolean
If true, == comparison will be used. If false, .equals() comparison will be used.
identity
)

Returns true if this array contains all the specified values.

public boolean
containsAny(Array<? extends T>
May contains nulls.
values
,
boolean
If true, == comparison will be used. If false, .equals() comparison will be used.
identity
)

Returns true if this array contains any the specified values.

public T[]

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 boolean
equalsIdentity(Object object)

Uses == for comparison of each item.

public T
first()

Returns the first item.

public T
get(int index)

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public int

Returns:

An index of first occurrence of value in array or -1 if no such value exists
indexOf
(T
May be null.
value
,
boolean
If true, == comparison will be used. If false, .equals() comparison will be used.
identity
)

Returns the index of first occurrence of value in the array, or -1 if no such value exists.

public void
insert(int index, T 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 Array.ArrayIterator<T>
iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator for the items in the array.

public int

Returns:

An index of last occurrence of value in array or -1 if no such value exists
lastIndexOf
(T
May be null.
value
,
boolean
If true, == comparison will be used. If false, .equals() comparison will be used.
identity
)

Returns an index of last occurrence of value in array or -1 if no such value exists.

public boolean
notEmpty()

Returns true if the array has one or more items.

public static <T> Array<T>
of(ArraySupplier<T[]> arraySupplier)

public static <T> Array<T>
of(boolean ordered, int capacity, ArraySupplier<T[]> arraySupplier)

public static <T> Array<T>
of(Class<T> arrayType)
Deprecated Use Array#of(ArraySupplier)

public static <T> Array<T>
of(boolean ordered, int capacity, Class<T> arrayType)

public T
peek()

Returns the last item.

public T
pop()

Removes and returns the last item.

public T
random()

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

public boolean

Returns:

true if this array was modified.
removeAll
(Array<? extends T> array, boolean
True to use ==, false to use .equals().
identity
)

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

public T
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

Returns:

true if value was found and removed, false otherwise
removeValue
(T
May be null.
value
,
boolean
If true, == comparison will be used. If false, .equals() comparison will be used.
identity
)

Removes the first instance of the specified value in the array.

public int

Returns:

the number of replacements done
replaceAll
(T
May be null.
value
,
boolean
If true, == comparison will be used. If false, .equals() comparison will be used.
identity
,
T
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
(T
May be null.
value
,
boolean
If true, == comparison will be used. If false, .equals() comparison will be used.
identity
,
T
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 T[]
resize(int newSize)

Creates a new backing array with the specified size containing the current items.

public void
public Iterable<T>
select(Predicate<T> predicate)

Returns an iterable for the selected items in the array.

public T

Returns:

the value of the Nth lowest ranked object.
selectRanked
(Comparator<T>
used for comparison
comparator
,
int
rank of desired object according to comparison, n is based on ordinal numbers, not array indices. for min value use 1, for max value use size of array, using 0 results in runtime exception.
kthLowest
)

Selects the nth-lowest element from the Array according to Comparator ranking.

public int
selectRankedIndex(Comparator<T>
used for comparison
comparator
,
int
rank of desired object according to comparison, n is based on ordinal numbers, not array indices. for min value use 1, for max value use size of array, using 0 results in runtime exception.
kthLowest
)

Returns: the index of the Nth lowest ranked object.

public void
set(int index, T value)

public T[]

Returns:

items
setSize
(int newSize)

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

public T[]

Returns:

items
shrink
()

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

public void
public void
sort()

Sorts this array.

public void
sort(Comparator<? super T> comparator)

Sorts the array.

public void
swap(int first, int second)

public T[]
toArray()

Returns the items as an array.

public T[]
toArray(ArraySupplier<T[]> arraySupplier)

public <V> V[]
toArray(Class<V> type)
Deprecated Use Array#toArray(ArraySupplier) instead

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 <T> Array<T>
with(T... array)

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

itemsback to summary
public T[] items

Provides direct access to the underlying array. If the Array's generic type is not Object, this field may only be accessed if the Array#Array(boolean, int, ArraySupplier) constructor was used.

iterableback to summary
private transient Array.ArrayIterable<T> iterable
orderedback to summary
public boolean ordered
predicateIterableback to summary
private transient Predicate.PredicateIterable<T> predicateIterable
sizeback to summary
public int size

Constructor Detail

Arrayback to summary
public Array()

Creates an ordered array with a capacity of 16.

Arrayback to summary
public Array(int capacity)

Creates an ordered array with the specified capacity.

Arrayback to summary
public Array(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.

Arrayback to summary
public Array(boolean ordered, int capacity, ArraySupplier<T[]> arraySupplier)

Creates a new array with items with the specified supplier.

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.

Arrayback to summary
public Array(ArraySupplier<T[]> arraySupplier)

Creates an ordered array with items with the specified supplier and a capacity of 16.

Arrayback to summary
public Array(boolean ordered, int capacity, Class<T> arrayType)

Creates a new array with items of the specified type.

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.

Annotations
@Deprecated
Arrayback to summary
public Array(Class<T> arrayType)

Deprecated

Use Array#Array(ArraySupplier) instead

Creates an ordered array with items of the specified type and a capacity of 16.

Annotations
@Deprecated
Arrayback to summary
public Array(Array<? extends T> array)

Creates a new array containing the elements in the specified array. The new array will have the same type of backing array and will be ordered if the specified 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.

Arrayback to summary
public Array(T[] array)

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

Arrayback to summary
public Array(boolean ordered, T[] array, int start, int count)

Creates a new array containing the elements in the specified array. The new array will have the same type of backing 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(T value)
addback to summary
public void add(T value1, T value2)
addback to summary
public void add(T value1, T value2, T value3)
addback to summary
public void add(T value1, T value2, T value3, T value4)
addAllback to summary
public void addAll(Array<? extends T> array)
addAllback to summary
public void addAll(Array<? extends T> array, int start, int count)
addAllback to summary
public void addAll(T... array)
addAllback to summary
public void addAll(T[] array, int start, int count)
clearback to summary
public void clear()
containsback to summary
public boolean contains(T value, boolean identity)

Returns true if this array contains the specified value.

Parameters
value:T

May be null.

identity:boolean

If true, == comparison will be used. If false, .equals() comparison will be used.

containsAllback to summary
public boolean containsAll(Array<? extends T> values, boolean identity)

Returns true if this array contains all the specified values.

Parameters
values:Array<? extends T>

May contains nulls.

identity:boolean

If true, == comparison will be used. If false, .equals() comparison will be used.

containsAnyback to summary
public boolean containsAny(Array<? extends T> values, boolean identity)

Returns true if this array contains any the specified values.

Parameters
values:Array<? extends T>

May contains nulls.

identity:boolean

If true, == comparison will be used. If false, .equals() comparison will be used.

ensureCapacityback to summary
public T[] 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:T[]

items

equalsback to summary
public boolean equals(Object object)

Overrides java.lang.Object.equals.

Returns false if either array is unordered.

equalsIdentityback to summary
public boolean equalsIdentity(Object object)

Uses == for comparison of each item. Returns false if either array is unordered.

firstback to summary
public T first()

Returns the first item.

getback to summary
public T 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.
indexOfback to summary
public int indexOf(T value, boolean identity)

Returns the index of first occurrence of value in the array, or -1 if no such value exists.

Parameters
value:T

May be null.

identity:boolean

If true, == comparison will be used. If false, .equals() comparison will be used.

Returns:int

An index of first occurrence of value in array or -1 if no such value exists

insertback to summary
public void insert(int index, T 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.

iteratorback to summary
public Array.ArrayIterator<T> iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator for the items in the array. Remove is supported.

If Collections#allocateIterators is false, the same iterator instance is returned each time this method is called. Use the ArrayIterator constructor for nested or multithreaded iteration.

lastIndexOfback to summary
public int lastIndexOf(T value, boolean identity)

Returns an index of last occurrence of value in array or -1 if no such value exists. Search is started from the end of an array.

Parameters
value:T

May be null.

identity:boolean

If true, == comparison will be used. If false, .equals() comparison will be used.

Returns:int

An index of last occurrence of value in array or -1 if no such value exists

notEmptyback to summary
public boolean notEmpty()

Returns true if the array has one or more items.

ofback to summary
public static <T> Array<T> of(ArraySupplier<T[]> arraySupplier)
See Also
Array(ArraySupplier)
ofback to summary
public static <T> Array<T> of(boolean ordered, int capacity, ArraySupplier<T[]> arraySupplier)
See Also
Array(boolean, int, ArraySupplier)
ofback to summary
public static <T> Array<T> of(Class<T> arrayType)

Deprecated

Use Array#of(ArraySupplier)

Annotations
@Deprecated
See Also
Array(Class)
ofback to summary
public static <T> Array<T> of(boolean ordered, int capacity, Class<T> arrayType)
Annotations
@Deprecated
See Also
Array(boolean, int, Class)
peekback to summary
public T peek()

Returns the last item.

popback to summary
public T pop()

Removes and returns the last item.

randomback to summary
public T random()

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

Annotations
@Null
removeAllback to summary
public boolean removeAll(Array<? extends T> array, boolean identity)

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

Parameters
identity:boolean

True to use ==, false to use .equals().

Returns:boolean

true if this array was modified.

removeIndexback to summary
public T 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(T value, boolean identity)

Removes the first instance of the specified value in the array.

Parameters
value:T

May be null.

identity:boolean

If true, == comparison will be used. If false, .equals() comparison will be used.

Returns:boolean

true if value was found and removed, false otherwise

replaceAllback to summary
public int replaceAll(T value, boolean identity, T replacement)

Returns the number of replacements done.

Parameters
value:T

May be null.

identity:boolean

If true, == comparison will be used. If false, .equals() comparison will be used.

replacement:T

all occurrences of value will be replaced by this replacement

Returns:int

the number of replacements done

replaceFirstback to summary
public boolean replaceFirst(T value, boolean identity, T replacement)

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

Parameters
value:T

May be null.

identity:boolean

If true, == comparison will be used. If false, .equals() comparison will be used.

replacement:T

the first value will be replaced by this replacement if found

Returns:boolean

if value was found and replaced

resizeback to summary
protected T[] resize(int newSize)

Creates a new backing array with the specified size containing the current items.

reverseback to summary
public void reverse()
selectback to summary
public Iterable<T> select(Predicate<T> predicate)

Returns an iterable for the selected items in the array. Remove is supported, but not between hasNext() and next().

If Collections#allocateIterators is false, the same iterable instance is returned each time this method is called. Use the Predicate.PredicateIterable constructor for nested or multithreaded iteration.

selectRankedback to summary
public T selectRanked(Comparator<T> comparator, int kthLowest)

Selects the nth-lowest element from the Array according to Comparator ranking. This might partially sort the Array. The array must have a size greater than 0, or a com.badlogic.gdx.utils.GdxRuntimeException will be thrown.

Parameters
comparator:Comparator<T>

used for comparison

kthLowest:int

rank of desired object according to comparison, n is based on ordinal numbers, not array indices. for min value use 1, for max value use size of array, using 0 results in runtime exception.

Returns:T

the value of the Nth lowest ranked object.

See Also
Select
selectRankedIndexback to summary
public int selectRankedIndex(Comparator<T> comparator, int kthLowest)
Parameters
comparator:Comparator<T>

used for comparison

kthLowest:int

rank of desired object according to comparison, n is based on ordinal numbers, not array indices. for min value use 1, for max value use size of array, using 0 results in runtime exception.

Returns:int

the index of the Nth lowest ranked object.

See Also
Array#selectRanked(java.util.Comparator, int)
setback to summary
public void set(int index, T value)
setSizeback to summary
public T[] setSize(int newSize)

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

Returns:T[]

items

shrinkback to summary
public T[] 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:T[]

items

shuffleback to summary
public void shuffle()
sortback to summary
public void sort()

Sorts this array. The array elements must implement Comparable. This method is not thread safe (uses Sort#instance()).

sortback to summary
public void sort(Comparator<? super T> comparator)

Sorts the array. This method is not thread safe (uses Sort#instance()).

swapback to summary
public void swap(int first, int second)
toArrayback to summary
public T[] toArray()

Returns the items as an array. Note the array is typed, so the Array(ArraySupplier) constructor must have been used. Otherwise use toArray(ArraySupplier) to specify the array type.

toArrayback to summary
public T[] toArray(ArraySupplier<T[]> arraySupplier)
toArrayback to summary
public <V> V[] toArray(Class<V> type)

Deprecated

Use Array#toArray(ArraySupplier) instead

Annotations
@Deprecated
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 <T> Array<T> with(T... array)
See Also
Array(Object[])
com.badlogic.gdx.utils back to summary

public Class Array.ArrayIterable<T>

extends Object
implements Iterable<T>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable

Field Summary

Modifier and TypeField and Description
private final boolean
private final Array<T>
private transient Array.ArrayIterator<T>
private transient Array.ArrayIterator<T>

Constructor Summary

AccessConstructor and Description
public
ArrayIterable(Array<T> array)

public
ArrayIterable(Array<T> array, boolean allowRemove)

Method Summary

Modifier and TypeMethod and Description
public Array.ArrayIterator<T>
iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allowRemoveback to summary
private final boolean allowRemove
arrayback to summary
private final Array<T> array
iterator1back to summary
private transient Array.ArrayIterator<T> iterator1
iterator2back to summary
private transient Array.ArrayIterator<T> iterator2

Constructor Detail

ArrayIterableback to summary
public ArrayIterable(Array<T> array)
ArrayIterableback to summary
public ArrayIterable(Array<T> array, boolean allowRemove)

Method Detail

iteratorback to summary
public Array.ArrayIterator<T> iterator()

Implements java.lang.Iterable.iterator.

Doc from java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

See Also
Collections#allocateIterators
com.badlogic.gdx.utils back to summary

public Class Array.ArrayIterator<T>

extends Object
implements Iterator<T>, Iterable<T>
Class Inheritance
All Implemented Interfaces
java.lang.Iterable, java.util.Iterator

Field Summary

Modifier and TypeField and Description
private final boolean
private final Array<T>
pack-priv int
pack-priv boolean

Constructor Summary

AccessConstructor and Description
public
ArrayIterator(Array<T> array)

public
ArrayIterator(Array<T> array, boolean allowRemove)

Method Summary

Modifier and TypeMethod and Description
public boolean
hasNext()

Implements java.util.Iterator.hasNext.

Returns true if the iteration has more elements.

public Array.ArrayIterator<T>
iterator()

Implements java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

public T
next()

Implements java.util.Iterator.next.

Returns the next element in the iteration.

public void
remove()

Overrides default java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation).

public void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

allowRemoveback to summary
private final boolean allowRemove
arrayback to summary
private final Array<T> array
indexback to summary
pack-priv int index
validback to summary
pack-priv boolean valid

Constructor Detail

ArrayIteratorback to summary
public ArrayIterator(Array<T> array)
ArrayIteratorback to summary
public ArrayIterator(Array<T> array, boolean allowRemove)

Method Detail

hasNextback to summary
public boolean hasNext()

Implements java.util.Iterator.hasNext.

Doc from java.util.Iterator.hasNext.

Returns true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.)

iteratorback to summary
public Array.ArrayIterator<T> iterator()

Implements java.lang.Iterable.iterator.

Doc from java.lang.Iterable.iterator.

Returns an iterator over elements of type T.

nextback to summary
public T next()

Implements java.util.Iterator.next.

Doc from java.util.Iterator.next.

Returns the next element in the iteration.

removeback to summary
public void remove()

Overrides default java.util.Iterator.remove.

Doc from java.util.Iterator.remove.

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next.

The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.

The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

resetback to summary
public void reset()