Package com.badlogic.gdx.utils
Class Array<T>
java.lang.Object
com.badlogic.gdx.utils.Array<T>
- All Implemented Interfaces:
Iterable<T>
- Direct Known Subclasses:
DelayedRemovalArray
,SnapshotArray
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).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static class
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionArray()
Creates an ordered array with a capacity of 16.Array
(boolean ordered, int capacity) Array
(boolean ordered, int capacity, ArraySupplier<T[]> arraySupplier) Creates a new array withitems
with the specified supplier.Deprecated.Creates a new array containing the elements in the specified array.Array
(int capacity) Creates an ordered array with the specified capacity.Creates a new array containing the elements in the specified array.Array
(ArraySupplier<T[]> arraySupplier) Creates an ordered array withitems
with the specified supplier and a capacity of 16.Deprecated.UseArray(ArraySupplier)
insteadCreates a new ordered array containing the elements in the specified array. -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
void
void
void
void
void
void
void
clear()
boolean
Returns true if this array contains the specified value.boolean
containsAll
(Array<? extends T> values, boolean identity) Returns true if this array contains all the specified values.boolean
containsAny
(Array<? extends T> values, boolean identity) Returns true if this array contains any the specified values.T[]
ensureCapacity
(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items.boolean
Returns false if either array is unordered.boolean
equalsIdentity
(Object object) Uses == for comparison of each item.first()
Returns the first item.get
(int index) int
hashCode()
int
Returns the index of first occurrence of value in the array, or -1 if no such value exists.void
void
insertRange
(int index, int count) Inserts the specified number of items at the specified index.boolean
isEmpty()
Returns true if the array is empty.iterator()
Returns an iterator for the items in the array.int
lastIndexOf
(T value, boolean identity) Returns an index of last occurrence of value in array or -1 if no such value exists.boolean
notEmpty()
Returns true if the array has one or more items.static <T> Array<T>
of
(boolean ordered, int capacity, ArraySupplier<T[]> arraySupplier) static <T> Array<T>
Deprecated.static <T> Array<T>
of
(ArraySupplier<T[]> arraySupplier) static <T> Array<T>
Deprecated.peek()
Returns the last item.pop()
Removes and returns the last item.random()
Returns a random item from the array, or null if the array is empty.boolean
Removes from this array the first instance of each element contained in the specified array.removeIndex
(int index) Removes and returns the item at the specified index.void
removeRange
(int start, int end) Removes the items between the specified indices, inclusive.boolean
removeValue
(T value, boolean identity) Removes the first instance of the specified value in the array.int
replaceAll
(T value, boolean identity, T replacement) Returns the number of replacements done.boolean
replaceFirst
(T value, boolean identity, T replacement) Returns true if the specified value was replaced successfully with the replacementprotected T[]
resize
(int newSize) Creates a new backing array with the specified size containing the current items.void
reverse()
Returns an iterable for the selected items in the array.selectRanked
(Comparator<T> comparator, int kthLowest) Selects the nth-lowest element from the Array according to Comparator ranking.int
selectRankedIndex
(Comparator<T> comparator, int kthLowest) void
T[]
setSize
(int newSize) Sets the array size, leaving any values beyond the current size null.T[]
shrink()
Reduces the size of the backing array to the size of the actual items.void
shuffle()
void
sort()
Sorts this array.void
sort
(Comparator<? super T> comparator) Sorts the array.void
swap
(int first, int second) T[]
toArray()
Returns the items as an array.T[]
toArray
(ArraySupplier<T[]> arraySupplier) <V> V[]
Deprecated.UsetoArray(ArraySupplier)
insteadtoString()
void
truncate
(int newSize) Reduces the size of the array to the specified size.static <T> Array<T>
with
(T... array) Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
items
Provides direct access to the underlying array. If the Array's generic type is not Object, this field may only be accessed if theArray(boolean, int, Class)
constructor was used. -
size
public int size -
ordered
public boolean ordered
-
-
Constructor Details
-
Array
public Array()Creates an ordered array with a capacity of 16. -
Array
public Array(int capacity) Creates an ordered array with the specified capacity. -
Array
public Array(boolean ordered, int capacity) - Parameters:
ordered
- If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.capacity
- Any elements added beyond this will cause the backing array to be grown.
-
Array
Creates a new array withitems
with the specified supplier.- Parameters:
ordered
- If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.capacity
- Any elements added beyond this will cause the backing array to be grown.
-
Array
Creates an ordered array withitems
with the specified supplier and a capacity of 16. -
Array
Deprecated.UseArray(boolean, int, ArraySupplier)
insteadCreates a new array withitems
of the specified type.- Parameters:
ordered
- If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.capacity
- Any elements added beyond this will cause the backing array to be grown.
-
Array
Deprecated.UseArray(ArraySupplier)
insteadCreates an ordered array withitems
of the specified type and a capacity of 16. -
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. -
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. -
Array
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
- If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
-
-
Method Details
-
add
-
add
-
add
-
add
-
addAll
-
addAll
-
addAll
-
addAll
-
get
-
set
-
insert
-
insertRange
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. -
swap
public void swap(int first, int second) -
replaceFirst
Returns true if the specified value was replaced successfully with the replacement- Parameters:
value
- May be null.identity
- If true, == comparison will be used. If false, .equals() comparison will be used.replacement
- the first value will be replaced by this replacement if found- Returns:
- if value was found and replaced
-
replaceAll
Returns the number of replacements done.- Parameters:
value
- May be null.identity
- If true, == comparison will be used. If false, .equals() comparison will be used.replacement
- all occurrences of value will be replaced by this replacement- Returns:
- the number of replacements done
-
contains
Returns true if this array contains the specified value.- Parameters:
value
- May be null.identity
- If true, == comparison will be used. If false, .equals() comparison will be used.
-
containsAll
Returns true if this array contains all the specified values.- Parameters:
values
- May contains nulls.identity
- If true, == comparison will be used. If false, .equals() comparison will be used.
-
containsAny
Returns true if this array contains any the specified values.- Parameters:
values
- May contains nulls.identity
- If true, == comparison will be used. If false, .equals() comparison will be used.
-
indexOf
Returns the index of first occurrence of value in the array, or -1 if no such value exists.- Parameters:
value
- May be null.identity
- If true, == comparison will be used. If false, .equals() comparison will be used.- Returns:
- An index of first occurrence of value in array or -1 if no such value exists
-
lastIndexOf
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
- May be null.identity
- If true, == comparison will be used. If false, .equals() comparison will be used.- Returns:
- An index of last occurrence of value in array or -1 if no such value exists
-
removeValue
Removes the first instance of the specified value in the array.- Parameters:
value
- May be null.identity
- If true, == comparison will be used. If false, .equals() comparison will be used.- Returns:
- true if value was found and removed, false otherwise
-
removeIndex
Removes and returns the item at the specified index. -
removeRange
public void removeRange(int start, int end) Removes the items between the specified indices, inclusive. -
removeAll
Removes from this array the first instance of each element contained in the specified array.- Parameters:
identity
- True to use ==, false to use .equals().- Returns:
- true if this array was modified.
-
pop
Removes and returns the last item. -
peek
Returns the last item. -
first
Returns the first item. -
notEmpty
public boolean notEmpty()Returns true if the array has one or more items. -
isEmpty
public boolean isEmpty()Returns true if the array is empty. -
clear
public void clear() -
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:
items
-
ensureCapacity
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:
items
-
setSize
Sets the array size, leaving any values beyond the current size null.- Returns:
items
-
resize
Creates a new backing array with the specified size containing the current items. -
sort
public void sort()Sorts this array. The array elements must implementComparable
. This method is not thread safe (usesSort.instance()
). -
sort
Sorts the array. This method is not thread safe (usesSort.instance()
). -
selectRanked
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 aGdxRuntimeException
will be thrown.- Parameters:
comparator
- used for comparisonkthLowest
- 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:
- the value of the Nth lowest ranked object.
- See Also:
-
selectRankedIndex
- Parameters:
comparator
- used for comparisonkthLowest
- 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:
- the index of the Nth lowest ranked object.
- See Also:
-
reverse
public void reverse() -
shuffle
public void shuffle() -
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 theArray.ArrayIterator
constructor for nested or multithreaded iteration. -
select
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 thePredicate.PredicateIterable
constructor for nested or multithreaded iteration. -
truncate
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. -
random
Returns a random item from the array, or null if the array is empty. -
toArray
Returns the items as an array. Note the array is typed, so theArray(Class)
constructor must have been used. Otherwise usetoArray(ArraySupplier)
to specify the array type. -
toArray
-
toArray
Deprecated.UsetoArray(ArraySupplier)
instead -
hashCode
public int hashCode() -
equals
Returns false if either array is unordered. -
equalsIdentity
Uses == for comparison of each item. Returns false if either array is unordered. -
toString
-
toString
-
of
- See Also:
-
of
- See Also:
-
of
Deprecated.- See Also:
-
of
Deprecated.- See Also:
-
with
- See Also:
-
Array(boolean, int, ArraySupplier)
instead