Class Array<T>

java.lang.Object
com.badlogic.gdx.utils.Array<T>
All Implemented Interfaces:
Iterable<T>
Direct Known Subclasses:
DelayedRemovalArray, SnapshotArray

public class Array<T> extends Object implements Iterable<T>
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 Classes
    Modifier and Type
    Class
    Description
    static class 
     
    static class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    T[]
    Provides direct access to the underlying array.
    boolean
     
    int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    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 with items with the specified supplier.
    Array(boolean ordered, int capacity, Class arrayType)
    Deprecated.
    Array(boolean ordered, T[] array, int start, int count)
    Creates a new array containing the elements in the specified array.
    Array(int capacity)
    Creates an ordered array with the specified capacity.
    Array(Array<T> array)
    Creates a new array containing the elements in the specified array.
    Array(ArraySupplier<T[]> arraySupplier)
    Creates an ordered array with items with the specified supplier and a capacity of 16.
    Array(Class arrayType)
    Deprecated.
    Array(T[] array)
    Creates a new ordered array containing the elements in the specified array.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(T value)
     
    void
    add(T value1, T value2)
     
    void
    add(T value1, T value2, T value3)
     
    void
    add(T value1, T value2, T value3, T value4)
     
    void
    addAll(Array<? extends T> array)
     
    void
    addAll(Array<? extends T> array, int start, int count)
     
    void
    addAll(T... array)
     
    void
    addAll(T[] array, int start, int count)
     
    void
     
    boolean
    contains(T value, boolean identity)
    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
    equals(Object object)
    Returns false if either array is unordered.
    boolean
    Uses == for comparison of each item.
    Returns the first item.
    get(int index)
     
    int
     
    int
    indexOf(T value, boolean identity)
    Returns the index of first occurrence of value in the array, or -1 if no such value exists.
    void
    insert(int index, T value)
     
    void
    insertRange(int index, int count)
    Inserts the specified number of items at the specified index.
    boolean
    Returns true if the array is empty.
    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
    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>
    of(boolean ordered, int capacity, Class<T> arrayType)
    static <T> Array<T>
    of(ArraySupplier<T[]> arraySupplier)
     
    static <T> Array<T>
    of(Class<T> arrayType)
    Deprecated.
    Returns the last item.
    pop()
    Removes and returns the last item.
    Returns a random item from the array, or null if the array is empty.
    boolean
    removeAll(Array<? extends T> array, boolean identity)
    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 replacement
    protected T[]
    resize(int newSize)
    Creates a new backing array with the specified size containing the current items.
    void
     
    select(Predicate<T> predicate)
    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
    set(int index, T value)
     
    T[]
    setSize(int newSize)
    Sets the array size, leaving any values beyond the current size null.
    T[]
    Reduces the size of the backing array to the size of the actual items.
    void
     
    void
    Sorts this array.
    void
    sort(Comparator<? super T> comparator)
    Sorts the array.
    void
    swap(int first, int second)
     
    T[]
    Returns the items as an array.
    T[]
    toArray(ArraySupplier<T[]> arraySupplier)
     
    <V> V[]
    toArray(Class<V> type)
    Deprecated.
     
    toString(String separator)
     
    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

      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(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

      public Array(boolean ordered, int capacity, ArraySupplier<T[]> arraySupplier)
      Creates a new array with items 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

      public Array(ArraySupplier<T[]> arraySupplier)
      Creates an ordered array with items with the specified supplier and a capacity of 16.
    • Array

      @Deprecated public Array(boolean ordered, int capacity, Class arrayType)
      Deprecated.
      Creates a new array with items 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 public Array(Class arrayType)
      Deprecated.
      Creates an ordered array with items of the specified type and a capacity of 16.
    • Array

      public Array(Array<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.
    • Array

      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.
    • Array

      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 - If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
  • Method Details

    • add

      public void add(T value)
    • add

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

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

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

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

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

      public void addAll(T... array)
    • addAll

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

      public T get(int index)
    • set

      public void set(int index, T value)
    • insert

      public void insert(int index, T value)
    • 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

      public boolean replaceFirst(@Null T value, boolean identity, T replacement)
      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

      public int replaceAll(@Null T value, boolean identity, @Null T replacement)
      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

      public boolean contains(@Null T value, boolean identity)
      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

      public boolean containsAll(Array<? extends T> values, boolean identity)
      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

      public boolean containsAny(Array<? extends T> values, boolean identity)
      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

      public int indexOf(@Null T value, boolean identity)
      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

      public int lastIndexOf(@Null 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 - 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

      public boolean removeValue(@Null T value, boolean identity)
      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

      public T removeIndex(int index)
      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

      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 - True to use ==, false to use .equals().
      Returns:
      true if this array was modified.
    • pop

      public T pop()
      Removes and returns the last item.
    • peek

      public T peek()
      Returns the last item.
    • first

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

      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:
      items
    • ensureCapacity

      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:
      items
    • setSize

      public T[] setSize(int newSize)
      Sets the array size, leaving any values beyond the current size null.
      Returns:
      items
    • resize

      protected T[] resize(int newSize)
      Creates a new backing array with the specified size containing the current items.
    • sort

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

      public void sort(Comparator<? super T> comparator)
      Sorts the array. This method is not thread safe (uses Sort.instance()).
    • selectRanked

      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 GdxRuntimeException will be thrown.
      Parameters:
      comparator - used for comparison
      kthLowest - 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

      public int selectRankedIndex(Comparator<T> comparator, int kthLowest)
      Parameters:
      comparator - used for comparison
      kthLowest - 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

      public Array.ArrayIterator<T> 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 Array.ArrayIterator constructor for nested or multithreaded iteration.

      Specified by:
      iterator in interface Iterable<T>
    • select

      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.

    • 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

      @Null public T random()
      Returns a random item from the array, or null if the array is empty.
    • toArray

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

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

      @Deprecated public <V> V[] toArray(Class<V> type)
      Deprecated.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object object)
      Returns false if either array is unordered.
      Overrides:
      equals in class Object
    • equalsIdentity

      public boolean equalsIdentity(Object object)
      Uses == for comparison of each item. Returns false if either array is unordered.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public String toString(String separator)
    • of

      public static <T> Array<T> of(ArraySupplier<T[]> arraySupplier)
      See Also:
    • of

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

      @Deprecated public static <T> Array<T> of(Class<T> arrayType)
      Deprecated.
      See Also:
    • of

      @Deprecated public static <T> Array<T> of(boolean ordered, int capacity, Class<T> arrayType)
      See Also:
    • with

      public static <T> Array<T> with(T... array)
      See Also: