Class ArrayUtils


  • public class ArrayUtils
    extends Object
    Static utility methods for operating on arrays. Note: You can also find some methods for printing arrays that are tables in StringUtils. (Search for makeTextTable, etc.)
    Author:
    Huy Nguyen (htnguyen@cs.stanford.edu), Michel Galley (mgalley@stanford.edu)
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> Set<T> asImmutableSet​(T[] a)
      Return an immutable Set containing the same elements as the specified array.
      static List<Integer> asList​(int[] array)
      needed because Arrays.asList() won't to autoboxing, so if you give it a primitive array you get a singleton list back with just that array as an element.
      static double[] asPrimitiveDoubleArray​(Collection<Double> d)  
      static int[] asPrimitiveIntArray​(Collection<Integer> d)  
      static <T> Set<T> asSet​(T[] a)
      Return a Set containing the same elements as the specified array.
      static <T extends Comparable<T>>
      int
      compareArrays​(T[] first, T[] second)
      Provides a consistent ordering over arrays.
      static int compareBooleanArrays​(boolean[] a1, boolean[] a2)  
      static <T> T[] concatenate​(T[] first, T[] second)
      Concatenates two arrays and returns the result
      static <T> boolean contains​(T[] a, T o)
      Returns true iff object o equals (not ==) some element of array a.
      static double[] copy​(double[] d)  
      static double[][] copy​(double[][] d)  
      static double[][][] copy​(double[][][] d)  
      static float[] copy​(float[] d)  
      static float[][] copy​(float[][] d)  
      static float[][][] copy​(float[][][] d)  
      static int[] copy​(int[] i)  
      static int[][] copy​(int[][] i)  
      static long[] copy​(long[] arr)  
      static short[] copy​(short[] arr)  
      static int[] deltaDecode​(byte[] deltaEncoded)  
      static int[] deltaDecode​(byte[] deltaEncoded, int startIndex, int endIndex)  
      static List<Integer> deltaDecodeList​(byte[] deltaEncoded)  
      static List<Integer> deltaDecodeList​(byte[] deltaEncoded, int startIndex, int endIndex)  
      static byte[] deltaEncode​(int[] orig)  
      static List<Byte> deltaEncodeList​(int[] orig)  
      static boolean equalContents​(int[][] xs, int[][] ys)
      Tests two int[][] arrays for having equal contents.
      static boolean equalContents​(int[] xs, int[] ys)
      tests two int[] arrays for having equal contents
      static boolean equals​(boolean[][] xs, boolean[][] ys)
      Tests two boolean[][] arrays for having equal contents.
      static boolean equals​(double[][] xs, double[][] ys)
      Tests two double[][] arrays for having equal contents.
      static void fill​(boolean[][][][] d, boolean val)  
      static void fill​(boolean[][][] d, boolean val)  
      static void fill​(boolean[][] d, boolean val)  
      static void fill​(double[][][][] d, double val)  
      static void fill​(double[][][] d, double val)  
      static void fill​(double[][] d, double val)  
      static <T> T[] filter​(T[] original, Predicate<? super T> filter)
      Returns an array with only the elements accepted by filter
      Implementation notes: creates two arrays, calls filter once for each element, does not alter original
      static double[] flatten​(double[][] array)  
      static int[] gapDecode​(byte[] gapEncoded)  
      static int[] gapDecode​(byte[] gapEncoded, int startIndex, int endIndex)  
      static List<Integer> gapDecodeList​(byte[] gapEncoded)  
      static List<Integer> gapDecodeList​(byte[] gapEncoded, int startIndex, int endIndex)  
      static byte[] gapEncode​(int[] orig)  
      static List<Byte> gapEncodeList​(int[] orig)  
      static List<Integer> getSubListIndex​(Object[] tofind, Object[] tokens)  
      static List<Integer> getSubListIndex​(Object[] tofind, Object[] tokens, Predicate<Pair> matchingFunction)
      If tofind is a part of tokens, it finds the ****starting index***** of tofind in tokens If tofind is not a sub-array of tokens, then it returns null note that tokens sublist should have the exact elements and order as in tofind
      static <T> int indexOf​(T[] array, T object)
      Returns the index of the first occurrence of the specified element in this array, or -1 if this array does not contain the element.
      static <T> int indexOf​(T[] array, T object, int startIndex)
      Returns the index of the first occurrence of the specified element in this array starting at the specified index, or -1 if this array does not contain the element.
      static double[] normalize​(double[] ar)
      Returns a new array which has the numbers in the input array L1-normalized.
      static double[] removeAt​(double[] array, int index)
      Removes the element at the specified index from the array, and returns a new array containing the remaining elements.
      static Object[] removeAt​(Object[] array, int index)
      Removes the element at the specified index from the array, and returns a new array containing the remaining elements.
      static Object[] subArray​(Object[] arr, int startindexInclusive, int endindexExclusive)  
      static double[][] to2D​(double[] array, int dim1Size)  
      static double[][] to2D​(double[] array, int dim1Size, int dim2Size)  
      static double[] toDouble​(float[] a)
      Casts to a double array
      static double[] toDouble​(int[] array)
      Casts to a double array.
      static double[] toDoubleArray​(String[] in)  
      static char[] toPrimitive​(Character[] in)  
      static char[] toPrimitive​(Character[] in, char valueForNull)  
      static double[] toPrimitive​(Double[] in)  
      static double[] toPrimitive​(Double[] in, double valueForNull)  
      static int[] toPrimitive​(Integer[] in)  
      static int[] toPrimitive​(Integer[] in, int valueForNull)  
      static long[] toPrimitive​(Long[] in)  
      static long[] toPrimitive​(Long[] in, long valueForNull)  
      static short[] toPrimitive​(Short[] in)  
      static short[] toPrimitive​(Short[] in, short valueForNull)  
      static String toString​(boolean[][] b)  
      static String toString​(double[][] b)  
      static String toString​(double[] doubles, String glue)  
      static String toString​(int[][] a)  
    • Method Detail

      • gapEncode

        public static byte[] gapEncode​(int[] orig)
      • gapEncodeList

        public static List<Byte> gapEncodeList​(int[] orig)
      • gapDecode

        public static int[] gapDecode​(byte[] gapEncoded)
      • gapDecode

        public static int[] gapDecode​(byte[] gapEncoded,
                                      int startIndex,
                                      int endIndex)
      • gapDecodeList

        public static List<Integer> gapDecodeList​(byte[] gapEncoded)
      • gapDecodeList

        public static List<Integer> gapDecodeList​(byte[] gapEncoded,
                                                  int startIndex,
                                                  int endIndex)
      • deltaEncode

        public static byte[] deltaEncode​(int[] orig)
      • deltaEncodeList

        public static List<Byte> deltaEncodeList​(int[] orig)
      • deltaDecode

        public static int[] deltaDecode​(byte[] deltaEncoded)
      • deltaDecode

        public static int[] deltaDecode​(byte[] deltaEncoded,
                                        int startIndex,
                                        int endIndex)
      • deltaDecodeList

        public static List<Integer> deltaDecodeList​(byte[] deltaEncoded)
      • deltaDecodeList

        public static List<Integer> deltaDecodeList​(byte[] deltaEncoded,
                                                    int startIndex,
                                                    int endIndex)
      • flatten

        public static double[] flatten​(double[][] array)
      • to2D

        public static double[][] to2D​(double[] array,
                                      int dim1Size)
      • to2D

        public static double[][] to2D​(double[] array,
                                      int dim1Size,
                                      int dim2Size)
      • removeAt

        public static double[] removeAt​(double[] array,
                                        int index)
        Removes the element at the specified index from the array, and returns a new array containing the remaining elements. If index is invalid, returns array unchanged.
      • removeAt

        public static Object[] removeAt​(Object[] array,
                                        int index)
        Removes the element at the specified index from the array, and returns a new array containing the remaining elements. If index is invalid, returns array unchanged. Uses reflection to determine the type of the array and returns an array of the appropriate type.
      • toString

        public static String toString​(int[][] a)
      • equalContents

        public static boolean equalContents​(int[][] xs,
                                            int[][] ys)
        Tests two int[][] arrays for having equal contents.
        Returns:
        true iff for each i, equalContents(xs[i],ys[i]) is true
      • equals

        public static boolean equals​(double[][] xs,
                                     double[][] ys)
        Tests two double[][] arrays for having equal contents.
        Returns:
        true iff for each i, equals(xs[i],ys[i]) is true
      • equalContents

        public static boolean equalContents​(int[] xs,
                                            int[] ys)
        tests two int[] arrays for having equal contents
        Returns:
        true iff xs and ys have equal length, and for each i, xs[i]==ys[i]
      • equals

        public static boolean equals​(boolean[][] xs,
                                     boolean[][] ys)
        Tests two boolean[][] arrays for having equal contents.
        Returns:
        true iff for each i, Arrays.equals(xs[i],ys[i]) is true
      • contains

        public static <T> boolean contains​(T[] a,
                                           T o)
        Returns true iff object o equals (not ==) some element of array a.
      • concatenate

        public static <T> T[] concatenate​(T[] first,
                                          T[] second)
        Concatenates two arrays and returns the result
      • filter

        public static <T> T[] filter​(T[] original,
                                     Predicate<? super T> filter)
        Returns an array with only the elements accepted by filter
        Implementation notes: creates two arrays, calls filter once for each element, does not alter original
      • asSet

        public static <T> Set<T> asSet​(T[] a)
        Return a Set containing the same elements as the specified array.
      • asImmutableSet

        public static <T> Set<T> asImmutableSet​(T[] a)
        Return an immutable Set containing the same elements as the specified array. Arrays with 0 or 1 elements are special cased to return the efficient small sets from the Collections class.
      • fill

        public static void fill​(double[][] d,
                                double val)
      • fill

        public static void fill​(double[][][] d,
                                double val)
      • fill

        public static void fill​(double[][][][] d,
                                double val)
      • fill

        public static void fill​(boolean[][] d,
                                boolean val)
      • fill

        public static void fill​(boolean[][][] d,
                                boolean val)
      • fill

        public static void fill​(boolean[][][][] d,
                                boolean val)
      • toDouble

        public static double[] toDouble​(float[] a)
        Casts to a double array
      • toDouble

        public static double[] toDouble​(int[] array)
        Casts to a double array.
      • asList

        public static List<Integer> asList​(int[] array)
        needed because Arrays.asList() won't to autoboxing, so if you give it a primitive array you get a singleton list back with just that array as an element.
      • asPrimitiveDoubleArray

        public static double[] asPrimitiveDoubleArray​(Collection<Double> d)
      • asPrimitiveIntArray

        public static int[] asPrimitiveIntArray​(Collection<Integer> d)
      • copy

        public static long[] copy​(long[] arr)
      • copy

        public static int[] copy​(int[] i)
      • copy

        public static int[][] copy​(int[][] i)
      • copy

        public static short[] copy​(short[] arr)
      • copy

        public static double[] copy​(double[] d)
      • copy

        public static double[][] copy​(double[][] d)
      • copy

        public static double[][][] copy​(double[][][] d)
      • copy

        public static float[] copy​(float[] d)
      • copy

        public static float[][] copy​(float[][] d)
      • copy

        public static float[][][] copy​(float[][][] d)
      • toString

        public static String toString​(double[][] b)
      • toString

        public static String toString​(boolean[][] b)
      • toPrimitive

        public static long[] toPrimitive​(Long[] in)
      • toPrimitive

        public static int[] toPrimitive​(Integer[] in)
      • toPrimitive

        public static short[] toPrimitive​(Short[] in)
      • toPrimitive

        public static char[] toPrimitive​(Character[] in)
      • toPrimitive

        public static double[] toPrimitive​(Double[] in)
      • toPrimitive

        public static long[] toPrimitive​(Long[] in,
                                         long valueForNull)
      • toPrimitive

        public static int[] toPrimitive​(Integer[] in,
                                        int valueForNull)
      • toPrimitive

        public static short[] toPrimitive​(Short[] in,
                                          short valueForNull)
      • toPrimitive

        public static char[] toPrimitive​(Character[] in,
                                         char valueForNull)
      • toDoubleArray

        public static double[] toDoubleArray​(String[] in)
      • toPrimitive

        public static double[] toPrimitive​(Double[] in,
                                           double valueForNull)
      • compareArrays

        public static <T extends Comparable<T>> int compareArrays​(T[] first,
                                                                  T[] second)
        Provides a consistent ordering over arrays. First compares by the first element. If that element is equal, the next element is considered, and so on. This is the array version of CollectionUtils.compareLists(java.util.List<T>, java.util.List<T>) and uses the same logic when the arrays are of different lengths.
      • indexOf

        public static <T> int indexOf​(T[] array,
                                      T object,
                                      int startIndex)
        Returns the index of the first occurrence of the specified element in this array starting at the specified index, or -1 if this array does not contain the element.
        Parameters:
        array - Array to search
        object - Object to look for
        startIndex - Start index
        Returns:
        index of the specified element in the array, or -1 if the array does not contain the element
      • indexOf

        public static <T> int indexOf​(T[] array,
                                      T object)
        Returns the index of the first occurrence of the specified element in this array, or -1 if this array does not contain the element.
        Parameters:
        array - Array to search
        object - Object to look for
        Returns:
        index of the specified element in the array, or -1 if the array does not contain the element
      • getSubListIndex

        public static List<Integer> getSubListIndex​(Object[] tofind,
                                                    Object[] tokens,
                                                    Predicate<Pair> matchingFunction)
        If tofind is a part of tokens, it finds the ****starting index***** of tofind in tokens If tofind is not a sub-array of tokens, then it returns null note that tokens sublist should have the exact elements and order as in tofind
        Parameters:
        tofind - array you want to find in tokens
        tokens -
        matchingFunction - function that takes (tofindtoken, token) pair and returns whether they match
        Returns:
        starting index of the sublist
      • normalize

        public static double[] normalize​(double[] ar)
        Returns a new array which has the numbers in the input array L1-normalized.
        Parameters:
        ar - Input array
        Returns:
        New array that has L1 normalized form of input array
      • subArray

        public static Object[] subArray​(Object[] arr,
                                        int startindexInclusive,
                                        int endindexExclusive)
      • compareBooleanArrays

        public static int compareBooleanArrays​(boolean[] a1,
                                               boolean[] a2)
      • toString

        public static String toString​(double[] doubles,
                                      String glue)