Class AoArrays


  • public class AoArrays
    extends Object
    General-purpose array utilities and constants.
    Author:
    AO Industries, Inc.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean allEquals​(byte[] b, int off, int len, byte value)
      Checks if all the values in the provided range are equal to value.
      static <E> Set<E> asUnmodifiableSet​(E... array)
      Gets an unmodifiable set view with the contents of a backing array.
      static int compare​(byte[] ba1, byte[] ba2)
      Compares two byte[].
      static boolean equals​(byte[] b1, byte[] b2, int off, int len)
      Checks if the subrange of two byte arrays is equal.
      static boolean equals​(byte[] b1, int off1, byte[] b2, int off2, int len)
      Checks if the subrange of two byte arrays is equal.
      static int hashCode​(byte[] a, int off, int len)
      Computes hashCode compatible with Arrays.hashCode, but only across the given subset of the array.
      static int indexOf​(byte[] array, byte element)
      Finds the first index of an element or -1 if not found.
      static int indexOf​(byte[] array, byte element, int fromIndex)
      Finds the first index of an element or -1 if not found.
      static int indexOf​(char[] array, char element)
      Finds the first index of an element or -1 if not found.
      static int indexOf​(char[] array, char element, int fromIndex)
      Finds the first index of an element or -1 if not found.
      static int indexOf​(double[] array, double element)
      Finds the first index of an element or -1 if not found.
      static int indexOf​(double[] array, double element, int fromIndex)
      Finds the first index of an element or -1 if not found.
      static int indexOf​(float[] array, float element)
      Finds the first index of an element or -1 if not found.
      static int indexOf​(float[] array, float element, int fromIndex)
      Finds the first index of an element or -1 if not found.
      static int indexOf​(int[] array, int element)
      Finds the first index of an element or -1 if not found.
      static int indexOf​(int[] array, int element, int fromIndex)
      Finds the first index of an element or -1 if not found.
      static int indexOf​(long[] array, long element)
      Finds the first index of an element or -1 if not found.
      static int indexOf​(long[] array, long element, int fromIndex)
      Finds the first index of an element or -1 if not found.
      static int indexOf​(short[] array, short element)
      Finds the first index of an element or -1 if not found.
      static int indexOf​(short[] array, short element, int fromIndex)
      Finds the first index of an element or -1 if not found.
      static <E> int indexOf​(E[] array, E element)
      Finds the first index of an element or -1 if not found.
      static <E> int indexOf​(E[] array, E element, int fromIndex)
      Finds the first index of an element or -1 if not found.
      static int lastIndexOf​(byte[] array, byte element)
      Finds the last index of an element or -1 if not found.
      static int lastIndexOf​(byte[] array, byte element, int fromIndex)
      Finds the last index of an element or -1 if not found.
      static int lastIndexOf​(char[] array, char element)
      Finds the last index of an element or -1 if not found.
      static int lastIndexOf​(char[] array, char element, int fromIndex)
      Finds the last index of an element or -1 if not found.
      static int lastIndexOf​(double[] array, double element)
      Finds the last index of an element or -1 if not found.
      static int lastIndexOf​(double[] array, double element, int fromIndex)
      Finds the last index of an element or -1 if not found.
      static int lastIndexOf​(float[] array, float element)
      Finds the last index of an element or -1 if not found.
      static int lastIndexOf​(float[] array, float element, int fromIndex)
      Finds the last index of an element or -1 if not found.
      static int lastIndexOf​(int[] array, int element)
      Finds the last index of an element or -1 if not found.
      static int lastIndexOf​(int[] array, int element, int fromIndex)
      Finds the last index of an element or -1 if not found.
      static int lastIndexOf​(long[] array, long element)
      Finds the last index of an element or -1 if not found.
      static int lastIndexOf​(long[] array, long element, int fromIndex)
      Finds the last index of an element or -1 if not found.
      static int lastIndexOf​(short[] array, short element)
      Finds the last index of an element or -1 if not found.
      static int lastIndexOf​(short[] array, short element, int fromIndex)
      Finds the last index of an element or -1 if not found.
      static <E> int lastIndexOf​(E[] array, E element)
      Finds the last index of an element or -1 if not found.
      static <E> int lastIndexOf​(E[] array, E element, int fromIndex)
      Finds the last index of an element or -1 if not found.
      static <T extends Comparable<? super T>>
      T
      maxNonNull​(T... values)
      Gets the maximum non-null value, or null if no non-null value.
      static <V> V[] merge​(Class<V> clazz, Collection<? extends Collection<? extends V>> collections, Comparator<? super V> comparator)
      Merges multiple already-sorted collections into one big array.
      static <E extends Comparable<? super E>>
      void
      sortParallelArrays​(E[] keys, Object[]... otherArrays)
      Sorts parallel arrays in-place.
    • Method Detail

      • equals

        public static boolean equals​(byte[] b1,
                                     byte[] b2,
                                     int off,
                                     int len)
        Checks if the subrange of two byte arrays is equal.
      • equals

        public static boolean equals​(byte[] b1,
                                     int off1,
                                     byte[] b2,
                                     int off2,
                                     int len)
        Checks if the subrange of two byte arrays is equal.
      • allEquals

        public static boolean allEquals​(byte[] b,
                                        int off,
                                        int len,
                                        byte value)
        Checks if all the values in the provided range are equal to value.
      • compare

        public static int compare​(byte[] ba1,
                                  byte[] ba2)
        Compares two byte[]. Shorter byte[] are ordered before longer when the shorter is a prefix of the longer. The comparison considers each byte as a value from 0-255.
      • merge

        public static <V> V[] merge​(Class<V> clazz,
                                    Collection<? extends Collection<? extends V>> collections,
                                    Comparator<? super V> comparator)
        Merges multiple already-sorted collections into one big array. Worst-case Complexity: 0 collections: constant 1 collection: O(n), where n is the number of elements in the collection 2 collection: O(n+m), where n is the number of elements in one collection, and m is the number of elements in the other collection 3+ collections: O(n*log(m)), where n is the total number of elements in all collections, and m is the number of collections
        Returns:
        Object[] of results.
      • asUnmodifiableSet

        @SafeVarargs
        public static <E> Set<E> asUnmodifiableSet​(E... array)
        Gets an unmodifiable set view with the contents of a backing array.

        Contains is implemented sequentially and is thus O(n). As a result, this is best used for very small sets.

        In order to have correct set semantics, the array must have unique values as determined by the element equals methods. This is not checked, however, and passing in an array with duplicate values will result in duplicate values on iteration and a size that doesn't match the number of unique values.

        Parameters:
        array - may be null, which will return null
        Returns:
        an unmodifiable set or null when array == null
      • indexOf

        public static <E> int indexOf​(E[] array,
                                      E element)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static <E> int indexOf​(E[] array,
                                      E element,
                                      int fromIndex)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static <E> int lastIndexOf​(E[] array,
                                          E element)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static <E> int lastIndexOf​(E[] array,
                                          E element,
                                          int fromIndex)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static <E extends Enum<E>> int indexOf​(E[] array,
                                                      E element)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static <E extends Enum<E>> int indexOf​(E[] array,
                                                      E element,
                                                      int fromIndex)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static <E extends Enum<E>> int lastIndexOf​(E[] array,
                                                          E element)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static <E extends Enum<E>> int lastIndexOf​(E[] array,
                                                          E element,
                                                          int fromIndex)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static int indexOf​(byte[] array,
                                  byte element)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static int indexOf​(byte[] array,
                                  byte element,
                                  int fromIndex)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static int lastIndexOf​(byte[] array,
                                      byte element)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static int lastIndexOf​(byte[] array,
                                      byte element,
                                      int fromIndex)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static int indexOf​(short[] array,
                                  short element)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static int indexOf​(short[] array,
                                  short element,
                                  int fromIndex)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static int lastIndexOf​(short[] array,
                                      short element)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static int lastIndexOf​(short[] array,
                                      short element,
                                      int fromIndex)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static int indexOf​(int[] array,
                                  int element)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static int indexOf​(int[] array,
                                  int element,
                                  int fromIndex)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static int lastIndexOf​(int[] array,
                                      int element)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static int lastIndexOf​(int[] array,
                                      int element,
                                      int fromIndex)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static int indexOf​(long[] array,
                                  long element)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static int indexOf​(long[] array,
                                  long element,
                                  int fromIndex)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static int lastIndexOf​(long[] array,
                                      long element)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static int lastIndexOf​(long[] array,
                                      long element,
                                      int fromIndex)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static int indexOf​(float[] array,
                                  float element)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static int indexOf​(float[] array,
                                  float element,
                                  int fromIndex)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static int lastIndexOf​(float[] array,
                                      float element)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static int lastIndexOf​(float[] array,
                                      float element,
                                      int fromIndex)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static int indexOf​(double[] array,
                                  double element)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static int indexOf​(double[] array,
                                  double element,
                                  int fromIndex)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static int lastIndexOf​(double[] array,
                                      double element)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static int lastIndexOf​(double[] array,
                                      double element,
                                      int fromIndex)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static int indexOf​(char[] array,
                                  char element)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • indexOf

        public static int indexOf​(char[] array,
                                  char element,
                                  int fromIndex)
        Finds the first index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static int lastIndexOf​(char[] array,
                                      char element)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • lastIndexOf

        public static int lastIndexOf​(char[] array,
                                      char element,
                                      int fromIndex)
        Finds the last index of an element or -1 if not found.
        Parameters:
        array - may be null, in which the element is never found
      • hashCode

        public static int hashCode​(byte[] a,
                                   int off,
                                   int len)
        Computes hashCode compatible with Arrays.hashCode, but only across the given subset of the array.
        Parameters:
        a - may be null, which will return 0
        See Also:
        Arrays.hashCode(byte[])
      • maxNonNull

        @SafeVarargs
        public static <T extends Comparable<? super T>> T maxNonNull​(T... values)
        Gets the maximum non-null value, or null if no non-null value.
        Parameters:
        values - may be null, which will return null
      • sortParallelArrays

        public static <E extends Comparable<? super E>> void sortParallelArrays​(E[] keys,
                                                                                Object[]... otherArrays)
        Sorts parallel arrays in-place. Sorts by the first array and updates all other arrays to match. Uses the natural sorting of the objects. All arrays must be the same length.

        The time complexity isn't too bad. Looks something like O((M+1)*N*log(N)), where M is the number of otherArrays and N is the number of keys. No crazy worst-case issues, at least.

        Parameters:
        keys - the values used to sort, may be duplicate
        otherArrays - the arrays to have reordered to match the sorting of the keys array.
        Throws:
        IllegalArgumentException - if any of otherArrays have a length different that the keys array.