Class ArrayUtils


  • public class ArrayUtils
    extends java.lang.Object

    Operations on arrays, primitive arrays (like int[]) and primitive wrapper arrays (like Integer[]).

    This class tries to handle null input gracefully. An exception will not be thrown for a null array input. However, an Object array that contains a null element may throw an exception. Each method documents its behavior.

    #ThreadSafe#

    Since:
    2.0
    • Constructor Summary

      Constructors 
      Constructor Description
      ArrayUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int[] add​(int[] array, int element)
      Copies the given array and adds the given element at the end of the new array.
      static boolean contains​(java.lang.Object[] array, java.lang.Object objectToFind)
      Checks if the object is in the given array.
      static boolean isSameLength​(java.lang.Object array1, java.lang.Object array2)
      Checks whether two arrays are the same length, treating null arrays as length 0.
      static <T> T[] remove​(T[] array, int index)
      Removes the element at the specified position from the specified array.
      static <T> T[] removeAll​(T[] array, int... indices)
      Removes the elements at the specified positions from the specified array.
      • Methods inherited from class java.lang.Object

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

      • EMPTY_BOOLEAN_ARRAY

        public static final boolean[] EMPTY_BOOLEAN_ARRAY
        An empty immutable boolean array.
      • EMPTY_BOOLEAN_OBJECT_ARRAY

        public static final java.lang.Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY
        An empty immutable Boolean array.
      • EMPTY_CHAR_ARRAY

        public static final char[] EMPTY_CHAR_ARRAY
        An empty immutable char array.
      • EMPTY_CLASS_ARRAY

        public static final java.lang.Class<?>[] EMPTY_CLASS_ARRAY
        An empty immutable Class array.
      • EMPTY_INT_ARRAY

        public static final int[] EMPTY_INT_ARRAY
        An empty immutable int array.
      • EMPTY_STRING_ARRAY

        public static final java.lang.String[] EMPTY_STRING_ARRAY
        An empty immutable String array.
    • Constructor Detail

      • ArrayUtils

        public ArrayUtils()
    • Method Detail

      • add

        public static int[] add​(int[] array,
                                int element)

        Copies the given array and adds the given element at the end of the new array.

        The new array contains the same elements of the input array plus the given element in the last position. The component type of the new array is the same as that of the input array.

        If the input array is null, a new one element array is returned whose component type is the same as the element.

         ArrayUtils.add(null, 0)   = [0]
         ArrayUtils.add([1], 0)    = [1, 0]
         ArrayUtils.add([1, 0], 1) = [1, 0, 1]
         
        Parameters:
        array - the array to copy and add the element to, may be null
        element - the object to add at the last index of the new array
        Returns:
        A new array containing the existing elements plus the new element
        Since:
        2.1
      • contains

        public static boolean contains​(java.lang.Object[] array,
                                       java.lang.Object objectToFind)

        Checks if the object is in the given array.

        The method returns false if a null array is passed in.

        Parameters:
        array - the array to search through
        objectToFind - the object to find
        Returns:
        true if the array contains the object
      • isSameLength

        public static boolean isSameLength​(java.lang.Object array1,
                                           java.lang.Object array2)

        Checks whether two arrays are the same length, treating null arrays as length 0.

        Any multi-dimensional aspects of the arrays are ignored.

        Parameters:
        array1 - the first array, may be null
        array2 - the second array, may be null
        Returns:
        true if length of arrays matches, treating null as an empty array
        Since:
        3.11
      • remove

        public static <T> T[] remove​(T[] array,
                                     int index)

        Removes the element at the specified position from the specified array. All subsequent elements are shifted to the left (subtracts one from their indices).

        This method returns a new array with the same elements of the input array except the element on the specified position. The component type of the returned array is always the same as that of the input array.

        If the input array is null, an IndexOutOfBoundsException will be thrown, because in that case no valid index can be specified.

         ArrayUtils.remove(["a"], 0)           = []
         ArrayUtils.remove(["a", "b"], 0)      = ["b"]
         ArrayUtils.remove(["a", "b"], 1)      = ["a"]
         ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"]
         
        Type Parameters:
        T - the component type of the array
        Parameters:
        array - the array to remove the element from, may not be null
        index - the position of the element to be removed
        Returns:
        A new array containing the existing elements except the element at the specified position.
        Throws:
        java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= array.length), or if the array is null.
        Since:
        2.1
      • removeAll

        public static <T> T[] removeAll​(T[] array,
                                        int... indices)

        Removes the elements at the specified positions from the specified array. All remaining elements are shifted to the left.

        This method returns a new array with the same elements of the input array except those at the specified positions. The component type of the returned array is always the same as that of the input array.

        If the input array is null, an IndexOutOfBoundsException will be thrown, because in that case no valid index can be specified.

         ArrayUtils.removeAll(["a", "b", "c"], 0, 2) = ["b"]
         ArrayUtils.removeAll(["a", "b", "c"], 1, 2) = ["a"]
         
        Type Parameters:
        T - the component type of the array
        Parameters:
        array - the array to remove the element from, may not be null
        indices - the positions of the elements to be removed
        Returns:
        A new array containing the existing elements except those at the specified positions.
        Throws:
        java.lang.IndexOutOfBoundsException - if any index is out of range (index < 0 || index >= array.length), or if the array is null.
        Since:
        3.0.1