Class Arrays


  • public final class Arrays
    extends Object
    A set of util methods to work with arrays. Note: For OpenL rules only! Don't use it in Java code.
    Author:
    Yury Molchan
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T[] addElement​(T[] array, int index, T element)  
      static <T> T[] addElement​(T[] array, int index, T... elements)
      Inserts the specified element at the specified position in the array.
      static <T> boolean isEmpty​(T[] array)
      Checks if an array is empty or null.
      static <T> boolean isNotEmpty​(T[] array)
      Checks if an array is NOT empty or null.
      static <T> int length​(T[] array)  
      static <T> boolean noNulls​(T... values)
      Returns true if the input elements exist, and they do not contain null elements.
      static <T,​E extends T>
      T[]
      removeElement​(T[] array, E... elements)
      Removes the first occurrence of the specified element from the specified array.
      static <T> T[] removeNulls​(T... elements)
      Return a new array without null elements
      static <T> T[] slice​(T[] values, int startIndexInclusive)  
      static <T> T[] slice​(T[] values, int startIndexInclusive, int endIndexExclusive)  
      static <T extends Comparable<T>>
      T[]
      sort​(T... values)
      Returns a copy of the specified array into ascending order, according to the natural ordering of its elements.
    • Method Detail

      • isEmpty

        public static <T> boolean isEmpty​(T[] array)
        Checks if an array is empty or null.
        Parameters:
        array - the array to test
        Returns:
        true if the array is empty or null
      • isNotEmpty

        public static <T> boolean isNotEmpty​(T[] array)
        Checks if an array is NOT empty or null.
        Parameters:
        array - the array to test
        Returns:
        true if the array is NOT empty or null
      • length

        public static <T> int length​(T[] array)
      • addElement

        public static <T> T[] addElement​(T[] array,
                                         int index,
                                         T... elements)

        Inserts the specified element at the specified position in the array. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

        This method returns a new array with the same elements of the input array plus the given 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, a new one element array is returned whose component type is the same as the element.

         Arrays.add(null, 0, null)      = [null]
         Arrays.add(null, 0, "a")       = ["a"]
         Arrays.add(["a"], 1, null)     = ["a", null]
         Arrays.add(["a"], 1, "b")      = ["a", "b"]
         Arrays.add(["a", "b"], 3, "c") = ["a", "b", "c"]
         
        Parameters:
        array - the array to add the element to, may be null
        index - the position of the new object
        elements - the objects to add
        Returns:
        A new array containing the existing elements and the new element
        Throws:
        IndexOutOfBoundsException - if the index is out of range (index < 0 || index > array.length).
      • addElement

        public static <T> T[] addElement​(T[] array,
                                         int index,
                                         T element)
      • slice

        public static <T> T[] slice​(T[] values,
                                    int startIndexInclusive)
      • slice

        public static <T> T[] slice​(T[] values,
                                    int startIndexInclusive,
                                    int endIndexExclusive)
      • removeElement

        public static <T,​E extends T> T[] removeElement​(T[] array,
                                                              E... elements)
        Removes the first occurrence of the specified element from the specified array. All subsequent elements are shifted to the left (substracts one from their indices). If the array does not contains such an element, no elements are removed from the array.

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

        ArrayUtils.removeElement(null, "a") = null
        ArrayUtils.removeElement([], "a") = []
        ArrayUtils.removeElement(["a"], "b") = ["a"]
        ArrayUtils.removeElement(["a", "b"], "a") = ["b"]
        ArrayUtils.removeElement(["a", "b", "a"], "a") = ["b", "a"]

        Parameters:
        array - the array to remove the element from, may be null
        elements -
        Returns:
        the element to be removed
      • removeNulls

        public static <T> T[] removeNulls​(T... elements)
        Return a new array without null elements
        Parameters:
        elements - whose null elements should be removed
        Returns:
        array without null elements
      • noNulls

        public static <T> boolean noNulls​(T... values)
        Returns true if the input elements exist, and they do not contain null elements.
      • sort

        public static <T extends Comparable<T>> T[] sort​(T... values)
        Returns a copy of the specified array into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).

        null values are kept in the tail.

        This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.

        Parameters:
        values - the array to be sorted
        Returns:
        a sorted array