Class MoreArrays


  • @ParametersAreNonnullByDefault
    public final class MoreArrays
    extends Object
    Static utility methods related to arrays.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int NO_INDEX
      The index value when an element is not found in a list or array: -1.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T[] add​(T[] array, int index, T element)
      Adds the specified element at the specified position in the array.
      static byte[] addAll​(byte[] array1, byte... array2)
      Adds all array2 at the end of the array1.
      static <T> T[] addAll​(T[] array, int index, Collection<? extends T> elements)
      Adds all elements at the end of the array.
      static <T> T[] append​(T[] array, T element)
      Adds the specified element at the last position in the array.
      static <T> boolean contains​(T[] array, T value)
      Checks if the value is in the given array.
      static <T> int indexOf​(T[] array, T value)
      Finds the index of the given value in the array.
      static <T> int lastIndexOf​(T[] array, T value)
      Finds the last index of the given value in the array.
      static <T> T[] newArray​(Class<?> type, int length)
      Creates a new array with the specified component type and length.
      static <T> T[] remove​(T[] array, int index)
      Removes the element at the specified index from the specified array.
      static <T> T[] resize​(T[] array, int length)
      Resizes the array with the given length.
      static <T> T[] set​(T[] array, int index, T value)
      Sets the specified element at the index in the array.
      static Byte[] toObject​(byte... primitiveArray)
      Converts an array of primitive types byte to an array of Byte.
      static byte[] toPrimitive​(Byte... boxedArray)
      Converts an array of Byte to an array of primitive types byte.
    • Field Detail

      • NO_INDEX

        public static final int NO_INDEX
        The index value when an element is not found in a list or array: -1.
        See Also:
        Constant Field Values
    • Method Detail

      • newArray

        @Nonnull
        public static <T> T[] newArray​(Class<?> type,
                                       @Nonnegative
                                       int length)
        Creates a new array with the specified component type and length.
        Type Parameters:
        T - the type of the new array
        Parameters:
        type - the class of the new array
        length - the length of the new array
        Returns:
        a new array
        Throws:
        NullPointerException - if the type is null
        IllegalArgumentException - if the length is negative
      • set

        @Nonnull
        public static <T> T[] set​(T[] array,
                                  @Nonnegative
                                  int index,
                                  @Nullable
                                  T value)
        Sets the specified element at the index in the array.

        This method is similar to:

        
         array[index] = value;
         return array;
         
        Type Parameters:
        T - the component type of the array
        Parameters:
        array - the array to set the element to
        index - the position of the object
        value - the object to set
        Returns:
        the array containing the existing elements and the new element
      • resize

        @Nonnull
        public static <T> T[] resize​(T[] array,
                                     @Nonnegative
                                     int length)
        Resizes the array with the given length.
        Type Parameters:
        T - the type of the new array
        Parameters:
        array - the array to resize
        length - the new length of the array
        Returns:
        a new array containing the existing elements with the new size
        Throws:
        NullPointerException - if the type is null
        IllegalArgumentException - if the length is lower than array.length
      • append

        @Nonnull
        public static <T> T[] append​(T[] array,
                                     @Nullable
                                     T element)
        Adds the specified element at the last position in the array.
        Type Parameters:
        T - the component type of the array
        Parameters:
        array - the array to add the element to
        element - the object to add
        Returns:
        a new array containing the existing elements and the new element
        Throws:
        NullPointerException - if the array is null
      • add

        @Nonnull
        public static <T> T[] add​(T[] array,
                                  @Nonnegative
                                  int index,
                                  @Nullable
                                  T element)
        Adds the specified element at the specified position in the array.
        Type Parameters:
        T - the component type of the array
        Parameters:
        array - the array to add the element to
        index - the position of the new object
        element - the object to add
        Returns:
        a new array containing the existing elements and the new element
        Throws:
        NullPointerException - if the array is null
        IndexOutOfBoundsException - if the index is out of range (index < 0 || index > array.length)
      • addAll

        @Nonnull
        public static <T> T[] addAll​(T[] array,
                                     int index,
                                     Collection<? extends T> elements)
        Adds all elements at the end of the array.
        Type Parameters:
        T - the component type of the array
        Parameters:
        array - the array to add the element to
        elements - the objects to add
        Returns:
        a new array containing the existing elements and the new elements
        Throws:
        NullPointerException - if any argument is null
      • addAll

        @Nonnull
        public static byte[] addAll​(byte[] array1,
                                    byte... array2)
        Adds all array2 at the end of the array1.
        Parameters:
        array1 - the array to add the element to
        array2 - the values to add
        Returns:
        a new array of bytes containing all the existing elements in arrays 1 and 2.
        Throws:
        NullPointerException - if any argument is null
      • remove

        @Nonnull
        public static <T> T[] remove​(T[] array,
                                     @Nonnegative
                                     int index)
        Removes the element at the specified index from the specified array.
        Type Parameters:
        T - the component type of the array
        Parameters:
        array - the array to remove the element from
        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:
        NullPointerException - if the array is null
        IndexOutOfBoundsException - if the index is out of range (index < 0 || index > array.length)
      • contains

        public static <T> boolean contains​(T[] array,
                                           @Nullable
                                           T value)
        Checks if the value is in the given array.
        Parameters:
        array - the array to search through
        value - the object to find
        Returns:
        true if the array contains the object, false otherwise
        Throws:
        NullPointerException - if the array is null
      • indexOf

        public static <T> int indexOf​(T[] array,
                                      @Nullable
                                      T value)
        Finds the index of the given value in the array.
        Parameters:
        array - the array to search through for the object
        value - the value to find, may be null
        Returns:
        the index of the value within the array, -1 if not found
        Throws:
        NullPointerException - if the array is null
      • lastIndexOf

        public static <T> int lastIndexOf​(T[] array,
                                          @Nullable
                                          T value)
        Finds the last index of the given value in the array.
        Parameters:
        array - the array to traverse for looking for the object
        value - the object to find, may be null
        Returns:
        the last index of the object within the array, -1 if not found
        Throws:
        NullPointerException - if the array is null
      • toPrimitive

        @Nonnull
        public static byte[] toPrimitive​(Byte... boxedArray)
        Converts an array of Byte to an array of primitive types byte.
        Parameters:
        boxedArray - the array of Byte to convert.
        Returns:
        an array of byte containing the same elements as boxedArray, in the same order, converted to primitives.
      • toObject

        public static Byte[] toObject​(byte... primitiveArray)
        Converts an array of primitive types byte to an array of Byte.
        Parameters:
        primitiveArray - the array of byte to convert.
        Returns:
        an array of Byte containing the same elements as primitiveArray, in the same order, converted to boxed types.