Class ArrayUtil


  • public final class ArrayUtil
    extends Object
    A collection of utility functions (static methods) operating on arrays. Complements what's found in java.util.Arrays.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T[] append​(T[] list, T item)
      Inserts specified item in specified array at the end of specified array.
      static <T> int find​(T[] list, T item)
      Searches specified array for specified item.
      static <T> int indexOf​(T[] list, T item)
      Searches specified array for specified item.
      static <T> T[] insert​(T[] list, int index, T item)
      static <T> T[] insert​(T[] list, int index, T[] items)
      Inserts specified items in specified array at specified index.
      static <T> T[] prepend​(T[] list, T item)
      Inserts specified item in specified array at the beginning of specified array.
      static <T> T[] remove​(T[] list, T item)
      Remove specified item from specified array.
      static <T> T[] removeAt​(T[] list, int index)
      Equivalent to remove(list, index, 1).
      static <T> T[] removeAt​(T[] list, int index, int count)
      Removes a range of items found at specified index from specified array.
      static <T> void reverse​(T[] list)
      Reverses the order of the items in the specified array.
      static <T> T[] subArray​(T[] list, int start)
      static <T> T[] subArray​(T[] list, int start, int end)
      Returns a sub-array of specified array.
      static <T> T[] trimToSize​(T[] list, int listSize)
      Returns specified array if its length is equal to specified size; otherwise returns a copy of specified array having specified size.
    • Method Detail

      • trimToSize

        public static final <T> T[] trimToSize​(T[] list,
                                               int listSize)
        Returns specified array if its length is equal to specified size; otherwise returns a copy of specified array having specified size.
        Type Parameters:
        T - the type of the components of the array
        Parameters:
        list - array whose size needs to be adjusted
        listSize - the size of the returned array
        Returns:
        an array having listSize components
      • subArray

        public static final <T> T[] subArray​(T[] list,
                                             int start,
                                             int end)
        Returns a sub-array of specified array.
        Type Parameters:
        T - the type of the components of the array
        Parameters:
        list - the array
        start - the begin index, inclusive
        end - the end index, exclusive
        Returns:
        the sub-array
        Throws:
        IndexOutOfBoundsException - if start or end are negative, if end is greater than list.length or if start is greater than end
      • insert

        public static final <T> T[] insert​(T[] list,
                                           int index,
                                           T[] items)
        Inserts specified items in specified array at specified index.
        Type Parameters:
        T - the type of the components of the array
        Parameters:
        list - list to be modified
        index - items are inserted at this position.

        Note that index may be equal to the size of the array. This means: insert at end.

        items - items to be inserted
        Returns:
        new array having original size+items.length components
      • prepend

        public static final <T> T[] prepend​(T[] list,
                                            T item)
        Inserts specified item in specified array at the beginning of specified array.
        Type Parameters:
        T - the type of the components of the array
        Parameters:
        list - list to be modified
        item - item to be inserted
        Returns:
        new array having original size+1 components
      • append

        public static final <T> T[] append​(T[] list,
                                           T item)
        Inserts specified item in specified array at the end of specified array.
        Type Parameters:
        T - the type of the components of the array
        Parameters:
        list - list to be modified
        item - item to be inserted
        Returns:
        new array having original size+1 components
      • remove

        public static final <T> T[] remove​(T[] list,
                                           T item)
        Remove specified item from specified array. Items are compared using equals() and not == (using indexOf(T[], T)).
        Type Parameters:
        T - the type of the components of the array
        Parameters:
        list - list to be modified
        item - item to be removed
        Returns:
        new array having original size-1 components
      • indexOf

        public static final <T> int indexOf​(T[] list,
                                            T item)
        Searches specified array for specified item. Unlike find(T[], T), items are compared using equals() and not ==.
        Type Parameters:
        T - the type of the components of the array
        Parameters:
        list - a list possibly containing item
        item - searched item
        Returns:
        the index of searched item if found; -1 otherwise
      • find

        public static final <T> int find​(T[] list,
                                         T item)
        Searches specified array for specified item. Unlike indexOf(T[], T), items are compared using == and not equals().
        Type Parameters:
        T - the type of the components of the array
        Parameters:
        list - a list possibly containing item
        item - searched item
        Returns:
        the index of searched item if found; -1 otherwise
      • removeAt

        public static final <T> T[] removeAt​(T[] list,
                                             int index)
        Equivalent to remove(list, index, 1).
      • removeAt

        public static final <T> T[] removeAt​(T[] list,
                                             int index,
                                             int count)
        Removes a range of items found at specified index from specified array.
        Type Parameters:
        T - the type of the components of the array
        Parameters:
        list - list to be modified
        index - items are removed at this position
        count - number of items to be removed
        Returns:
        new array having original size-count
      • reverse

        public static final <T> void reverse​(T[] list)
        Reverses the order of the items in the specified array.