Class ArrayUtils


  • public class ArrayUtils
    extends java.lang.Object
    Handy methods for resizing arrays, and other handy array methods This is a fresh implementation of the capacity methods, without the use of any prelicensed code.
    Author:
    Ben He, Rodrygo Santos
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static double GOLDEN_RATIO
      the Golden ration (φ).
    • Constructor Summary

      Constructors 
      Constructor Description
      ArrayUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int[] cast​(double[] arr)
      cast double[] to int[]
      static double[] cast​(float[] arr)
      cast float[] to double[]
      static double[] cast​(int[] arr)
      cast int[] to double[]
      static <T> boolean contains​(T[] input, T match)  
      static byte[] ensureCapacity​(byte[] array, int length)
      Grow an array to ensure it is the desired length.
      static byte[] ensureCapacity​(byte[] array, int length, int preserve)
      Grow an array to ensure it is the desired length.
      static byte[] grow​(byte[] array, int length)
      Grow an array to ensure it is at least the desired length.
      static byte[] grow​(byte[] array, int length, int preserve)
      Grow an array to ensure it is at least the desired length.
      static double[] grow​(double[] array, int length)
      grow array
      static int[] grow​(int[] array, int length)
      grow array
      static short[] grow​(short[] array, int length)
      grow array
      static byte[] growOrCreate​(byte[] arr, int len)  
      static int[] growOrCreate​(int[] arr, int len)  
      static int[] intersection​(int[] arr1, int[] arr2)
      return intersection between two int[]
      static int[][] invert​(int[][] input)  
      static java.lang.String join​(boolean[] in, java.lang.String join)
      Join some strings together.
      static java.lang.String join​(byte[] in, java.lang.String join)
      join string
      static java.lang.String join​(double[] in, java.lang.String join)
      join string
      static java.lang.String join​(int[] in, java.lang.String join)
      Join some strings together.
      static java.lang.String join​(java.lang.Object[] in, char join)
      Join some strings together.
      static java.lang.String join​(java.lang.String[] in, char join)
      Join some strings together.
      static java.lang.String join​(java.lang.String[] in, java.lang.String join)
      Join some strings together.
      static java.lang.String join​(java.lang.String[] in, java.lang.String join, int l, int r)
      Join some strings together.
      static int[] parseCommaDelimitedInts​(java.lang.String src)
      parse comma delimited int
      static java.lang.String[] parseCommaDelimitedString​(java.lang.String src)
      parse comma delimited string
      static int[] parseDelimitedInts​(java.lang.String src, java.lang.String sep)
      parse comma delimited int
      static java.lang.String[] parseDelimitedString​(java.lang.String src, java.lang.String delim)
      parse delimited string
      static java.lang.String[] parseDelimitedString​(java.lang.String src, java.lang.String[] delims)
      parse delimited string
      static void reverse​(double[] a)
      Reverse the order of an array of doubles
      static void reverse​(int[] a)
      Reverse the order of an array of ints
      static int[] union​(int[] arr1, int[] arr2)
      union 2 int[]
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • GOLDEN_RATIO

        protected static final double GOLDEN_RATIO
        the Golden ration (φ).
        See Also:
        Constant Field Values
    • Constructor Detail

      • ArrayUtils

        public ArrayUtils()
    • Method Detail

      • contains

        public static <T> boolean contains​(T[] input,
                                           T match)
      • invert

        public static int[][] invert​(int[][] input)
      • ensureCapacity

        public static byte[] ensureCapacity​(byte[] array,
                                            int length)
        Grow an array to ensure it is the desired length.
        Parameters:
        array - input array
        length - ensure array is this length
        Returns:
        new array with desired length
      • ensureCapacity

        public static byte[] ensureCapacity​(byte[] array,
                                            int length,
                                            int preserve)
        Grow an array to ensure it is the desired length. Only copy the first preserve elements from the input array
        Parameters:
        array - input array
        length - new desired length
        preserve - amount of old array to copy to new array in case of reallocation
        Returns:
        new array with desired length
      • grow

        public static byte[] grow​(byte[] array,
                                  int length)
        Grow an array to ensure it is at least the desired length. The golden ratio is involved in the new length
        Parameters:
        array - input array
        length - minimuim length of new array
        Returns:
        new array appropriately sized
      • growOrCreate

        public static int[] growOrCreate​(int[] arr,
                                         int len)
      • growOrCreate

        public static byte[] growOrCreate​(byte[] arr,
                                          int len)
      • reverse

        public static void reverse​(double[] a)
        Reverse the order of an array of doubles
      • reverse

        public static void reverse​(int[] a)
        Reverse the order of an array of ints
      • grow

        public static byte[] grow​(byte[] array,
                                  int length,
                                  int preserve)
        Grow an array to ensure it is at least the desired length. The golden ratio is involved in the new length. Only copy the first preserve elements from the input array.
        Parameters:
        array - input array
        length - minimuim length of new array
        Returns:
        new array appropriately sized
      • join

        public static java.lang.String join​(java.lang.String[] in,
                                            java.lang.String join)
        Join some strings together.
        Parameters:
        in - Strings to join
        join - Character or String to join by
      • join

        public static java.lang.String join​(java.lang.String[] in,
                                            java.lang.String join,
                                            int l,
                                            int r)
        Join some strings together.
        Parameters:
        in -
        join -
        l -
        r -
        Returns:
        String
      • join

        public static java.lang.String join​(java.lang.Object[] in,
                                            char join)
        Join some strings together.
        Parameters:
        in - Strings to join
        join - Character or String to join by
      • join

        public static java.lang.String join​(java.lang.String[] in,
                                            char join)
        Join some strings together.
        Parameters:
        in - Strings to join
        join - Character or String to join by
      • join

        public static java.lang.String join​(int[] in,
                                            java.lang.String join)
        Join some strings together.
        Parameters:
        in -
        join -
        Returns:
        String
      • join

        public static java.lang.String join​(boolean[] in,
                                            java.lang.String join)
        Join some strings together.
        Parameters:
        in -
        join -
        Returns:
        String
      • grow

        public static int[] grow​(int[] array,
                                 int length)
        grow array
        Parameters:
        array -
        length -
        Returns:
        int[]
      • grow

        public static double[] grow​(double[] array,
                                    int length)
        grow array
        Parameters:
        array -
        length -
        Returns:
        double[]
      • grow

        public static short[] grow​(short[] array,
                                   int length)
        grow array
        Parameters:
        array -
        length -
        Returns:
        short[]
      • parseCommaDelimitedString

        public static java.lang.String[] parseCommaDelimitedString​(java.lang.String src)
        parse comma delimited string
        Parameters:
        src -
        Returns:
        String[]
      • parseDelimitedString

        public static java.lang.String[] parseDelimitedString​(java.lang.String src,
                                                              java.lang.String delim)
        parse delimited string
        Parameters:
        src -
        delim -
        Returns:
        String[]
      • parseDelimitedString

        public static java.lang.String[] parseDelimitedString​(java.lang.String src,
                                                              java.lang.String[] delims)
        parse delimited string
        Parameters:
        src -
        delims -
        Returns:
        String[]
      • parseCommaDelimitedInts

        public static int[] parseCommaDelimitedInts​(java.lang.String src)
        parse comma delimited int
        Parameters:
        src -
        Returns:
        int[]
      • parseDelimitedInts

        public static int[] parseDelimitedInts​(java.lang.String src,
                                               java.lang.String sep)
        parse comma delimited int
        Parameters:
        src -
        sep -
        Returns:
        int[]
      • join

        public static java.lang.String join​(byte[] in,
                                            java.lang.String join)
        join string
        Parameters:
        in -
        join -
        Returns:
        String
      • join

        public static java.lang.String join​(double[] in,
                                            java.lang.String join)
        join string
        Parameters:
        in -
        join -
        Returns:
        String
      • cast

        public static int[] cast​(double[] arr)
        cast double[] to int[]
        Parameters:
        arr -
        Returns:
        int[]
      • cast

        public static double[] cast​(int[] arr)
        cast int[] to double[]
        Parameters:
        arr -
        Returns:
        double[]
      • cast

        public static double[] cast​(float[] arr)
        cast float[] to double[]
        Parameters:
        arr -
        Returns:
        double[]
      • intersection

        public static int[] intersection​(int[] arr1,
                                         int[] arr2)
        return intersection between two int[]
        Parameters:
        arr1 -
        arr2 -
        Returns:
        intersection
      • union

        public static int[] union​(int[] arr1,
                                  int[] arr2)
        union 2 int[]
        Parameters:
        arr1 -
        arr2 -
        Returns:
        int[]