Class ArrayUtil


  • public final class ArrayUtil
    extends java.lang.Object
    Utility class for operating on arrays as if they were collections. This is useful for critical paths where operations like add and remove are seldom used, but iterating is common and checkcast and indirection are comparatively expensive.

    In all cases the array being mutated is assumed to be full.

    In all cases reference equality is used.

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ArrayUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T[] add​(T[] oldElements, T elementToAdd)
      Add an element to an array resulting in a new array.
      static <T> T[] ensureCapacity​(T[] oldElements, int requiredLength)
      Ensure an array has the required capacity.
      static <T> T[] newArray​(T[] oldElements, int length)
      Allocate a new array of the same type as another array.
      static <T> T[] remove​(T[] oldElements, int index)
      Remove an element from an array resulting in a new array if the index was inside the array otherwise the old array.
      static <T> T[] remove​(T[] oldElements, T elementToRemove)
      Remove an element from an array resulting in a new array if the element was found otherwise the old array.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • UNKNOWN_INDEX

        public static final int UNKNOWN_INDEX
        Constant indicating an invalid/unknown array index.
        See Also:
        Constant Field Values
      • EMPTY_BOOLEAN_ARRAY

        public static final boolean[] EMPTY_BOOLEAN_ARRAY
        Empty boolean array.
      • EMPTY_BYTE_ARRAY

        public static final byte[] EMPTY_BYTE_ARRAY
        Empty byte array.
      • EMPTY_CHAR_ARRAY

        public static final char[] EMPTY_CHAR_ARRAY
        Empty char array.
      • EMPTY_SHORT_ARRAY

        public static final short[] EMPTY_SHORT_ARRAY
        Empty short array.
      • EMPTY_INT_ARRAY

        public static final int[] EMPTY_INT_ARRAY
        Empty int array.
      • EMPTY_FLOAT_ARRAY

        public static final float[] EMPTY_FLOAT_ARRAY
        Empty float array.
      • EMPTY_LONG_ARRAY

        public static final long[] EMPTY_LONG_ARRAY
        Empty boolean array.
      • EMPTY_DOUBLE_ARRAY

        public static final double[] EMPTY_DOUBLE_ARRAY
        Empty double array.
      • EMPTY_OBJECT_ARRAY

        public static final java.lang.Object[] EMPTY_OBJECT_ARRAY
        Empty Object array.
      • EMPTY_STRING_ARRAY

        public static final java.lang.String[] EMPTY_STRING_ARRAY
        Empty String array.
      • MAX_CAPACITY

        public static final int MAX_CAPACITY
        Maximum capacity to which an array can grow.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ArrayUtil

        private ArrayUtil()
    • Method Detail

      • add

        public static <T> T[] add​(T[] oldElements,
                                  T elementToAdd)
        Add an element to an array resulting in a new array.
        Type Parameters:
        T - type of the array.
        Parameters:
        oldElements - to have the new element added.
        elementToAdd - for the new array.
        Returns:
        a new array that is one bigger and containing the new element at the end.
      • remove

        public static <T> T[] remove​(T[] oldElements,
                                     T elementToRemove)
        Remove an element from an array resulting in a new array if the element was found otherwise the old array.

        Returns its input parameter if the element to remove isn't a member.

        Type Parameters:
        T - type of the array.
        Parameters:
        oldElements - to have the element removed from.
        elementToRemove - being searched for by identity semantics.
        Returns:
        a new array without the element if found otherwise the original array.
      • remove

        public static <T> T[] remove​(T[] oldElements,
                                     int index)
        Remove an element from an array resulting in a new array if the index was inside the array otherwise the old array.

        Returns the old elements array if the index isn't inside the array.

        Type Parameters:
        T - type of the array.
        Parameters:
        oldElements - to have the element removed from.
        index - to remove the element at.
        Returns:
        a new array without the element if the index is inside the array otherwise the original array.
      • newArray

        public static <T> T[] newArray​(T[] oldElements,
                                       int length)
        Allocate a new array of the same type as another array.
        Type Parameters:
        T - type of the array.
        Parameters:
        oldElements - on which the new array is based.
        length - of the new array.
        Returns:
        the new array of requested length.
      • ensureCapacity

        public static <T> T[] ensureCapacity​(T[] oldElements,
                                             int requiredLength)
        Ensure an array has the required capacity. Resizing only if needed.
        Type Parameters:
        T - type of the array.
        Parameters:
        oldElements - to ensure that are long enough.
        requiredLength - to ensure.
        Returns:
        an array of the required length.