Class ArrayUtil


  • public abstract class ArrayUtil
    extends Object
    Restriction:
    This class is not intended to be subclassed by clients.
    • Constructor Summary

      Constructors 
      Constructor Description
      ArrayUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <T> T[] addAll​(Class<T> c, T[] dest, Object[] source)
      Takes contents of the two arrays up to the first null element and concatenates them.
      static <T> void addAll​(Collection<? super T> collection, T... elements)
      Adds all elements of an array to a collection.
      static <T> T[] addAll​(T[] dest, Object[] source)
      Takes contents of the two arrays up to the first null element and concatenates them.
      static Object[] append​(Class<?> c, Object[] array, int currentLength, Object obj)
      Deprecated.
      static <T> T[] append​(Class<T> c, T[] array, T obj)
      Assumes that array contains nulls at the end, only.
      static <T> T[] append​(T[] array, T obj)
      Assumes that array contains nulls at the end, only.
      static <T> T[] appendAt​(Class<T> c, T[] array, int currentLength, T obj)
      Assumes that array contains nulls at the end, only.
      static <T> T[] appendAt​(T[] array, int currentLength, T obj)
      Assumes that array contains nulls at the end, only.
      static void compact​(Object[] array)
      Moves all null elements to the end of the array.
      static <T> boolean contains​(T[] array, T obj)
      Returns whether the specified array contains the specified object.
      static <T> boolean containsEqual​(T[] array, T obj)
      Assumes that array contains nulls at the end, only.
      static <S,​T>
      T[]
      convert​(Class<T> target, S[] source)
      Stores the specified array contents in a new array of specified runtime type.
      static <T> T[] filter​(T[] array, IUnaryPredicate<T> predicate)
      Filter the elements of an array down to just the ones that match the given predicate.
      static <T> int indexOf​(T[] array, T obj)
      Returns the index into the specified array of the specified object, or -1 if the array does not contain the object, or if the array is null.
      static <T> int indexOfEqual​(T[] array, T obj)
      Assumes that array contains nulls at the end, only.
      static <T> T[] prepend​(Class<T> c, T[] array, T obj)
      Inserts the obj at the beginning of the array, shifting the whole thing one index Assumes that array contains nulls at the end, only.
      static <T> T[] prepend​(T[] array, T obj)
      Inserts the obj at the beginning of the array, shifting the whole thing one index Assumes that array contains nulls at the end, only.
      static <T> void remove​(T[] array, T element)
      Removes first occurrence of element in array and moves objects behind up front.
      static <T> T[] removeDuplicates​(T[] array)
      Returns a new array that contains elements of the given array except duplicates and nulls.
      static <T> T[] removeFirst​(T[] array)
      Returns a new array that contains all of the elements of the given array except the first one.
      static <T> T[] removeNulls​(Class<T> c, T[] array)
      Removes all of the nulls from the array and returns a new array that contains all of the non-null elements.
      static <T> T[] removeNulls​(T[] array)
      Removes all of the nulls from the array and returns a new array that contains all of the non-null elements.
      static Object[] removeNullsAfter​(Class<?> c, Object[] array, int index)
      static void reverse​(Object[] array)
      Reverses order of elements in an array.
      static void reverse​(Object[] array, int fromIndex, int toIndex)
      Reverses order of elements in a subsection of an array.
      static int[] setInt​(int[] array, int idx, int val)  
      static <T> T[] trim​(Class<T> c, T[] array)  
      static <T> T[] trim​(Class<T> c, T[] array, boolean forceNew)
      Trims the given array and returns a new array with no null entries.
      static <T> T[] trim​(T[] array)
      Trims the given array and returns a new array with no null entries.
      static <T> T[] trim​(T[] array, boolean forceNew)
      Trims the given array and returns a new array with no null entries.
      static <T> T[] trim​(T[] array, int newLength)
      Trims the given array and returns a new array with no null entries.
      static <T> T[] trimAt​(Class<T> c, T[] array, int index)
      To improve performance, this method should be used instead of removeNulls(Class, Object[]) when all of the non-null elements in the array are grouped together at the beginning of the array and all of the s are at the end of the array.
    • Constructor Detail

      • ArrayUtil

        public ArrayUtil()
    • Method Detail

      • append

        public static <T> T[] append​(Class<T> c,
                                     T[] array,
                                     T obj)
        Assumes that array contains nulls at the end, only. Appends element after the last non-null element. If the array is null or not large enough, a larger one is allocated, using the given class object.
      • append

        public static <T> T[] append​(T[] array,
                                     T obj)
        Assumes that array contains nulls at the end, only. Appends element after the last non-null element. If the array is not large enough, a larger one is allocated. Null array is supported for backward compatibility only and only when T is Object.
      • appendAt

        public static <T> T[] appendAt​(Class<T> c,
                                       T[] array,
                                       int currentLength,
                                       T obj)
        Assumes that array contains nulls at the end, only. Appends object using the current length of the array.
        Since:
        5.1
      • appendAt

        public static <T> T[] appendAt​(T[] array,
                                       int currentLength,
                                       T obj)
        Assumes that array contains nulls at the end, only. Appends object using the current length of the array.
        Parameters:
        array - The array to append to. Not null
        currentLength - The number of non-null elements in the array
        obj - The object to append. Not null
        Returns:
        The modified array, which may be the same as the first parameter.
        Since:
        5.4
      • trim

        public static <T> T[] trim​(Class<T> c,
                                   T[] array,
                                   boolean forceNew)
        Trims the given array and returns a new array with no null entries. Assumes that nulls can be found at the end, only. if array is null, a new array of length 0 is returned if forceNew is true, a new array will always be created. if forceNew is false, a new array will only be created if the original array contained null entries.
        Parameters:
        c - the type of the new array
        array - the array to be trimmed
        forceNew -
      • trim

        public static <T> T[] trim​(Class<T> c,
                                   T[] array)
      • trim

        public static <T> T[] trim​(T[] array,
                                   boolean forceNew)
        Trims the given array and returns a new array with no null entries. Assumes that nulls can be found at the end, only. if forceNew is true, a new array will always be created. if forceNew is false, a new array will only be created if the original array contained null entries.
        Parameters:
        array - the array to be trimmed
        forceNew -
        Since:
        5.2
      • trim

        public static <T> T[] trim​(T[] array)
        Trims the given array and returns a new array with no null entries. Assumes that nulls can be found at the end, only.
        Parameters:
        array - the array to be trimmed
        Since:
        5.2
      • trim

        public static <T> T[] trim​(T[] array,
                                   int newLength)
        Trims the given array and returns a new array with no null entries. Assumes that nulls can be found at the end, only. Similar to trimAt(Class, Object[], int), but uses the new length instead of index.
        Parameters:
        array - the array to be trimmed
        newLength - the new length of the array, has to be less or equal than the current length.
        Returns:
        the modified array, which may be the same as the first parameter.
        Since:
        5.4
      • addAll

        public static <T> T[] addAll​(Class<T> c,
                                     T[] dest,
                                     Object[] source)
        Takes contents of the two arrays up to the first null element and concatenates them.
        Parameters:
        c - The type of the element of the returned array if there was not enough free space in the destination array.
        dest - The destination array. The elements of the source array are added to this array if there is enough free space in it. May be null.
        source - The source array. May not be null.
        Returns:
        The concatenated array, which may be the same as the first parameter.
      • addAll

        public static <T> T[] addAll​(T[] dest,
                                     Object[] source)
        Takes contents of the two arrays up to the first null element and concatenates them.
        Parameters:
        dest - The destination array. The elements of the source array are added to this array if there is enough free space in it. May be null.
        source - The source array. May not be null.
        Returns:
        The concatenated array, which may be the same as the first parameter.
        Since:
        5.2
      • addAll

        @SafeVarargs
        public static <T> void addAll​(Collection<? super T> collection,
                                      T... elements)
        Adds all elements of an array to a collection. For an ArrayList this method is slightly more efficient than java.util.Collections#addAll(Collection, T...).
        Since:
        5.4
      • contains

        public static <T> boolean contains​(T[] array,
                                           T obj)
        Returns whether the specified array contains the specified object. The comparison is by object identity.
        Parameters:
        array - the array to search
        obj - the object to search for
        Returns:
        true if the specified array contains the specified object, or the specified array is null
      • indexOf

        public static <T> int indexOf​(T[] array,
                                      T obj)
        Returns the index into the specified array of the specified object, or -1 if the array does not contain the object, or if the array is null. Comparison is by object identity.
        Parameters:
        array - the array to search
        obj - the object to search for
        Returns:
        the index into the specified array of the specified object, or -1 if the array does not contain the object, or if the array is null
      • containsEqual

        public static <T> boolean containsEqual​(T[] array,
                                                T obj)
        Assumes that array contains nulls at the end, only. Returns whether the specified array contains the specified object. Comparison is by object identity.
        Parameters:
        array - the array to search
        obj - the object to search for
        Returns:
        true if the specified array contains the specified object, or the specified array is null
      • indexOfEqual

        public static <T> int indexOfEqual​(T[] array,
                                           T obj)
        Assumes that array contains nulls at the end, only. Returns the index into the specified array of the specified object, or -1 if the array does not contain the object, or if the array is null. Comparison is by equals().
        Parameters:
        array - the array to search
        obj - the object to search for
        Returns:
        the index into the specified array of the specified object, or -1 if the array does not contain an equal object, or if the array is null
      • compact

        public static void compact​(Object[] array)
        Moves all null elements to the end of the array. The order of non-null elements is preserved.
        Since:
        5.4
      • removeNulls

        public static <T> T[] removeNulls​(Class<T> c,
                                          T[] array)
        Removes all of the nulls from the array and returns a new array that contains all of the non-null elements. If there are no nulls in the original array then the original array is returned. Note that this method should only be used when the placement of nulls within the array is unknown (due to performance efficiency).
      • removeNulls

        public static <T> T[] removeNulls​(T[] array)
        Removes all of the nulls from the array and returns a new array that contains all of the non-null elements.

        If there are no nulls in the original array then the original array is returned.

        Note that this method should only be used when the placement of nulls within the array is unknown (due to performance efficiency).

        Since:
        5.2
      • trimAt

        public static <T> T[] trimAt​(Class<T> c,
                                     T[] array,
                                     int index)
        To improve performance, this method should be used instead of removeNulls(Class, Object[]) when all of the non-null elements in the array are grouped together at the beginning of the array and all of the s are at the end of the array. The position of the last non-null element in the array must also be known.

        If you don't intend to pass null array, consider using trim(Object[], int) instead.

        Since:
        5.1
      • prepend

        public static <T> T[] prepend​(Class<T> c,
                                      T[] array,
                                      T obj)
        Inserts the obj at the beginning of the array, shifting the whole thing one index Assumes that array contains nulls at the end, only.
      • prepend

        public static <T> T[] prepend​(T[] array,
                                      T obj)
        Inserts the obj at the beginning of the array, shifting the whole thing one index Assumes that array contains nulls at the end, only. array must not be null.
        Since:
        5.2
      • remove

        public static <T> void remove​(T[] array,
                                      T element)
        Removes first occurrence of element in array and moves objects behind up front.
        Since:
        4.0
      • convert

        public static <S,​T> T[] convert​(Class<T> target,
                                              S[] source)
        Stores the specified array contents in a new array of specified runtime type.
        Parameters:
        target - the runtime type of the new array
        source - the source array
        Returns:
        the current array stored in a new array with the specified runtime type, or null if source is null.
      • reverse

        public static void reverse​(Object[] array)
        Reverses order of elements in an array.
        Parameters:
        array - the array
        Since:
        5.4
      • reverse

        public static void reverse​(Object[] array,
                                   int fromIndex,
                                   int toIndex)
        Reverses order of elements in a subsection of an array.
        Parameters:
        array - the array
        fromIndex - the index of the first affected element (inclusive)
        toIndex - the index of the last affected element (exclusive)
        Since:
        5.4
      • removeFirst

        public static <T> T[] removeFirst​(T[] array)
        Returns a new array that contains all of the elements of the given array except the first one.
        Throws:
        NullPointerException - if array is null
        IllegalArgumentException - if array is empty
        Since:
        5.1
      • removeDuplicates

        public static <T> T[] removeDuplicates​(T[] array)
        Returns a new array that contains elements of the given array except duplicates and nulls. Duplicates are determined by Object.equals(Object) method.
        Throws:
        NullPointerException - if array is null
        Since:
        5.5
      • setInt

        public static int[] setInt​(int[] array,
                                   int idx,
                                   int val)
      • filter

        public static <T> T[] filter​(T[] array,
                                     IUnaryPredicate<T> predicate)
        Filter the elements of an array down to just the ones that match the given predicate.
        Since:
        5.6