Class ArrayUtils


  • public final class ArrayUtils
    extends Object
    Set of utility methods to work with Arrays.
    Author:
    Alexey Stashok
    • Constructor Detail

      • ArrayUtils

        public ArrayUtils()
    • Method Detail

      • binarySearch

        public static int binarySearch​(int[] a,
                                       int fromIndex,
                                       int toIndex,
                                       int key)
      • addUnique

        public static <T> T[] addUnique​(T[] array,
                                        T element)
        Add unique element to the array.
        Type Parameters:
        T - type of the array element
        Parameters:
        array - array
        element - element to add
        Returns:
        array, which will contain the new element. Either new array instance, if passed array didn't contain the element, or the same array instance, if the element is already present in the array.
      • addUnique

        public static <T> T[] addUnique​(T[] array,
                                        T element,
                                        boolean replaceElementIfEquals)
        Add unique element to the array.
        Type Parameters:
        T - type of the array element
        Parameters:
        array - array
        element - element to add
        replaceElementIfEquals - if passed element is equal to some element in the array then depending on this parameter it will be replaced or not with the passed element.
        Returns:
        array, which will contain the new element. Either new array instance, if passed array didn't contain the element, or the same array instance, if the element is already present in the array.
      • remove

        public static <T> T[] remove​(T[] array,
                                     Object element)
        Removes the element from the array.
        Type Parameters:
        T - type of the array element
        Parameters:
        array - array
        element - the element to remove
        Returns:
        array, which won't contain the element. Either new array instance, if passed array contains the element, or the same array instance, if the element wasn't present in the array. null will be returned if the last element was removed from the passed array.
      • indexOf

        public static <T> int indexOf​(T[] array,
                                      Object element)
        Return the element index in the array.
        Type Parameters:
        T - type of the array element
        Parameters:
        array - array
        element - the element to look for.
        Returns:
        element's index, or -1 if element wasn't found.