Class Array<T>

  • All Implemented Interfaces:
    java.lang.Iterable<T>
    Direct Known Subclasses:
    DelayedRemovalArray, SnapshotArray

    public class Array<T>
    extends java.lang.Object
    implements java.lang.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).
    • Field Summary

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

      Constructors 
      Constructor Description
      Array()
      Creates an ordered array with a capacity of 16.
      Array​(boolean ordered, int capacity)  
      Array​(boolean ordered, int capacity, java.lang.Class arrayType)
      Creates a new array with items of the specified type.
      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<? extends T> array)
      Creates a new array containing the elements in the specified array.
      Array​(java.lang.Class arrayType)
      Creates an ordered array with items of the specified type and a capacity of 16.
      Array​(T[] array)
      Creates a new ordered array containing the elements in the specified array.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      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 clear()  
      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​(java.lang.Object object)
      Returns false if either array is unordered.
      boolean equalsIdentity​(java.lang.Object object)
      Uses == for comparison of each item.
      T first()
      Returns the first item.
      T get​(int index)  
      int hashCode()  
      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 isEmpty()
      Returns true if the array is empty.
      Array.ArrayIterator<T> 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, java.lang.Class<T> arrayType)  
      static <T> Array<T> of​(java.lang.Class<T> arrayType)  
      T peek()
      Returns the last item.
      T pop()
      Removes and returns the last item.
      T random()
      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 all of elements contained in the specified array.
      T 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.
      protected T[] resize​(int newSize)
      Creates a new backing array with the specified size containing the current items.
      void reverse()  
      java.lang.Iterable<T> select​(Predicate<T> predicate)
      Returns an iterable for the selected items in the array.
      T selectRanked​(java.util.Comparator<T> comparator, int kthLowest)
      Selects the nth-lowest element from the Array according to Comparator ranking.
      int selectRankedIndex​(java.util.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[] shrink()
      Reduces the size of the backing array to the size of the actual items.
      void shuffle()  
      void sort()
      Sorts this array.
      void sort​(java.util.Comparator<? super T> comparator)
      Sorts the array.
      void swap​(int first, int second)  
      T[] toArray()
      Returns the items as an array.
      <V> V[] toArray​(java.lang.Class<V> type)  
      java.lang.String toString()  
      java.lang.String toString​(java.lang.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 Detail

      • 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 Detail

      • 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,
                     java.lang.Class arrayType)
        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

        public Array​(java.lang.Class arrayType)
        Creates an ordered array with items of the specified type and a capacity of 16.
      • Array

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

      • 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)
      • 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 all of elements 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​(java.util.Comparator<? super T> comparator)
        Sorts the array. This method is not thread safe (uses Sort.instance()).
      • selectRanked

        public T selectRanked​(java.util.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:
        Select
      • selectRankedIndex

        public int selectRankedIndex​(java.util.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:
        selectRanked(java.util.Comparator, int)
      • 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 java.lang.Iterable<T>
      • select

        public java.lang.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(Class) to specify the array type.
      • toArray

        public <V> V[] toArray​(java.lang.Class<V> type)
      • hashCode

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

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

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

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toString

        public java.lang.String toString​(java.lang.String separator)
      • of

        public static <T> Array<T> of​(java.lang.Class<T> arrayType)
        See Also:
        Array(Class)