Class ArrayUtil


  • public final class ArrayUtil
    extends java.lang.Object
    A few of Utility methods for Arrays and ArrayLists
    Author:
    Bhagat
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ArrayUtil.InvalidSizeException
      An exception for when the array or list has the wrong size
    • Constructor Summary

      Constructors 
      Constructor Description
      ArrayUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <E> E[] combine​(E[] a, E[] b, E[] writeTo)
      combine two arrays into a third array
      static <E> E[] copy​(E[] arr, E[] arr2)
      Copies one array into another
      static <E> java.util.ArrayList<E> copy​(java.util.ArrayList<E> list)
      Copies the list into a new ArrayList at a different memory location
      static <E> int indexOf​(E[] arr, E obj)
      finds the index of a certain object in an array
      static <E> E[] map​(E[] arr, Function<E,​E> function)
      maps a function to each element in an array
      static <E,​T>
      T[]
      map​(E[] arr, Function<E,​T> function, T[] writeTo)
      Maps an array of type E to an array of type T using a function
      static <E> E[] newArrayFromArrayList​(java.util.ArrayList<E> list, E[] arr)
      This method creates an array from an ArrayList
      static java.util.ArrayList<java.lang.Boolean> newArrayList​(boolean... items)
      This method creates a new ArrayList with the elements specified by the parameters passed into the method
      static java.util.ArrayList<java.lang.Byte> newArrayList​(byte... items)
      This method creates a new ArrayList with the elements specified by the parameters passed into the method
      static java.util.ArrayList<java.lang.Character> newArrayList​(char... items)
      This method creates a new ArrayList with the elements specified by the parameters passed into the method
      static java.util.ArrayList<java.lang.Double> newArrayList​(double... items)
      This method creates a new ArrayList with the elements specified by the parameters passed into the method
      static java.util.ArrayList<java.lang.Float> newArrayList​(float... items)
      This method creates a new ArrayList with the elements specified by the parameters passed into the method
      static java.util.ArrayList<java.lang.Integer> newArrayList​(int... items)
      This method creates a new ArrayList with the elements specified by the parameters passed into the method
      static java.util.ArrayList<java.lang.Long> newArrayList​(long... items)
      This method creates a new ArrayList with the elements specified by the parameters passed into the method
      static java.util.ArrayList<java.lang.Short> newArrayList​(short... items)
      This method creates a new ArrayList with the elements specified by the parameters passed into the method
      static <E> java.util.ArrayList<E> newArrayList​(E... items)
      This method creates a new ArrayList with the elements specified by the parameters passed into the method
      static void printArr​(boolean... arr)
      Prints out the boolean array in an ArrayList format
      static void printArr​(byte... arr)
      Prints out the byte array in an ArrayList format
      static void printArr​(char... arr)
      Prints out the char array in an ArrayList format
      static void printArr​(double... arr)
      Prints out the double array in an ArrayList format
      static void printArr​(float... arr)
      Prints out the float array in an ArrayList format
      static void printArr​(int... arr)
      Prints out the int array in an ArrayList format
      static void printArr​(long... arr)
      Prints out the long array in an ArrayList format
      static void printArr​(short... arr)
      Prints out the short array in an ArrayList format
      static <E> void printArr​(E... arr)
      Prints out an array in an ArrayList format
      static double[] range​(double stop)
      creates an double array that ranges from 0 to the specified end
      static double[] range​(double start, double stop)
      creates an double array that ranges from a specified start to the specified end
      static double[] range​(double start, double stop, double step)
      creates an double array that ranges from a specified to the specified end with a specified step
      static float[] range​(float stop)
      creates an double array that ranges from 0 to the specified end
      static float[] range​(float start, float stop)
      creates an double array that ranges from a specified start to the specified end
      static float[] range​(float start, float stop, float step)
      creates an double array that ranges from a specified to the specified end with a specified step
      static <E> E[] reshape​(E[][] arr, E[] arr2)
      Reshapes a two dimensional array into a one dimensional array
      static <E> E[][] reshape​(E[] arr, E[][] arr2)
      Reshapes a one dimensional array into a two dimensional array
      • Methods inherited from class java.lang.Object

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

      • ArrayUtil

        public ArrayUtil()
    • Method Detail

      • copy

        public static <E> java.util.ArrayList<E> copy​(java.util.ArrayList<E> list)
        Copies the list into a new ArrayList at a different memory location
        Type Parameters:
        E - the array type
        Parameters:
        list - the list to be copied
        Returns:
        the copied list
      • copy

        public static <E> E[] copy​(E[] arr,
                                   E[] arr2)
        Copies one array into another
        Type Parameters:
        E - the array type
        Parameters:
        arr - the array to be copied
        arr2 - the array to copy to
        Returns:
        the copied array
      • reshape

        public static <E> E[][] reshape​(E[] arr,
                                        E[][] arr2)
                                 throws ArrayUtil.InvalidSizeException
        Reshapes a one dimensional array into a two dimensional array
        Type Parameters:
        E - the array type
        Parameters:
        arr - the 1D array
        arr2 - the 2D array
        Returns:
        the 2D array
        Throws:
        ArrayUtil.InvalidSizeException - thrown when the total number of elements are not the same in each array
      • reshape

        public static <E> E[] reshape​(E[][] arr,
                                      E[] arr2)
                               throws ArrayUtil.InvalidSizeException
        Reshapes a two dimensional array into a one dimensional array
        Type Parameters:
        E - the array type
        Parameters:
        arr - the 2D array
        arr2 - the 1D array
        Returns:
        the 1D array
        Throws:
        ArrayUtil.InvalidSizeException - thrown when the total number of elements are not the same in each array
      • map

        public static <E> E[] map​(E[] arr,
                                  Function<E,​E> function)
        maps a function to each element in an array
        Type Parameters:
        E - the array type
        Parameters:
        arr - the array
        function - the function
        Returns:
        the array after the mapping
      • map

        public static <E,​T> T[] map​(E[] arr,
                                          Function<E,​T> function,
                                          T[] writeTo)
        Maps an array of type E to an array of type T using a function
        Type Parameters:
        E - the original array type
        T - the final array type
        Parameters:
        arr - the original array
        function - the function
        writeTo - the array to write to
        Returns:
        the array written to
      • indexOf

        public static <E> int indexOf​(E[] arr,
                                      E obj)
        finds the index of a certain object in an array
        Type Parameters:
        E - the array type
        Parameters:
        arr - the array
        obj - the object to look for
        Returns:
        the index
      • newArrayFromArrayList

        public static <E> E[] newArrayFromArrayList​(java.util.ArrayList<E> list,
                                                    E[] arr)
        This method creates an array from an ArrayList
        Type Parameters:
        E - the array type
        Parameters:
        list - the array list to create the array from
        arr - the array to write to
        Returns:
        the new array created
      • newArrayList

        @SafeVarargs
        public static <E> java.util.ArrayList<E> newArrayList​(E... items)
        This method creates a new ArrayList with the elements specified by the parameters passed into the method
        Type Parameters:
        E - the array type
        Parameters:
        items - The elements that will fill the ArrayList to be returned
        Returns:
        A new ArrayList with the items specified from parameters
      • newArrayList

        @SafeVarargs
        public static java.util.ArrayList<java.lang.Boolean> newArrayList​(boolean... items)
        This method creates a new ArrayList with the elements specified by the parameters passed into the method
        Parameters:
        items - The boolean elements that will fill the ArrayList to be returned
        Returns:
        A new ArrayList with the items specified from parameters
      • newArrayList

        @SafeVarargs
        public static java.util.ArrayList<java.lang.Byte> newArrayList​(byte... items)
        This method creates a new ArrayList with the elements specified by the parameters passed into the method
        Parameters:
        items - The byte elements that will fill the ArrayList to be returned
        Returns:
        A new ArrayList with the items specified from parameters
      • newArrayList

        @SafeVarargs
        public static java.util.ArrayList<java.lang.Character> newArrayList​(char... items)
        This method creates a new ArrayList with the elements specified by the parameters passed into the method
        Parameters:
        items - The char elements that will fill the ArrayList to be returned
        Returns:
        A new ArrayList with the items specified from parameters
      • newArrayList

        @SafeVarargs
        public static java.util.ArrayList<java.lang.Short> newArrayList​(short... items)
        This method creates a new ArrayList with the elements specified by the parameters passed into the method
        Parameters:
        items - The short elements that will fill the ArrayList to be returned
        Returns:
        A new ArrayList with the items specified from parameters
      • newArrayList

        @SafeVarargs
        public static java.util.ArrayList<java.lang.Integer> newArrayList​(int... items)
        This method creates a new ArrayList with the elements specified by the parameters passed into the method
        Parameters:
        items - The int elements that will fill the ArrayList to be returned
        Returns:
        A new ArrayList with the items specified from parameters
      • newArrayList

        @SafeVarargs
        public static java.util.ArrayList<java.lang.Long> newArrayList​(long... items)
        This method creates a new ArrayList with the elements specified by the parameters passed into the method
        Parameters:
        items - The long elements that will fill the ArrayList to be returned
        Returns:
        A new ArrayList with the items specified from parameters
      • newArrayList

        @SafeVarargs
        public static java.util.ArrayList<java.lang.Float> newArrayList​(float... items)
        This method creates a new ArrayList with the elements specified by the parameters passed into the method
        Parameters:
        items - The float elements that will fill the ArrayList to be returned
        Returns:
        A new ArrayList with the items specified from parameters
      • newArrayList

        @SafeVarargs
        public static java.util.ArrayList<java.lang.Double> newArrayList​(double... items)
        This method creates a new ArrayList with the elements specified by the parameters passed into the method
        Parameters:
        items - The double elements that will fill the ArrayList to be returned
        Returns:
        A new ArrayList with the items specified from parameters
      • printArr

        @SafeVarargs
        public static <E> void printArr​(E... arr)
        Prints out an array in an ArrayList format
        Type Parameters:
        E - the array type
        Parameters:
        arr - Array to be printed
      • printArr

        @SafeVarargs
        public static void printArr​(boolean... arr)
        Prints out the boolean array in an ArrayList format
        Parameters:
        arr - Array to be printed
      • printArr

        @SafeVarargs
        public static void printArr​(byte... arr)
        Prints out the byte array in an ArrayList format
        Parameters:
        arr - Array to be printed
      • printArr

        @SafeVarargs
        public static void printArr​(char... arr)
        Prints out the char array in an ArrayList format
        Parameters:
        arr - Array to be printed
      • printArr

        @SafeVarargs
        public static void printArr​(short... arr)
        Prints out the short array in an ArrayList format
        Parameters:
        arr - Array to be printed
      • printArr

        @SafeVarargs
        public static void printArr​(int... arr)
        Prints out the int array in an ArrayList format
        Parameters:
        arr - Array to be printed
      • printArr

        @SafeVarargs
        public static void printArr​(long... arr)
        Prints out the long array in an ArrayList format
        Parameters:
        arr - Array to be printed
      • printArr

        @SafeVarargs
        public static void printArr​(float... arr)
        Prints out the float array in an ArrayList format
        Parameters:
        arr - Array to be printed
      • printArr

        @SafeVarargs
        public static void printArr​(double... arr)
        Prints out the double array in an ArrayList format
        Parameters:
        arr - Array to be printed
      • combine

        public static <E> E[] combine​(E[] a,
                                      E[] b,
                                      E[] writeTo)
        combine two arrays into a third array
        Type Parameters:
        E - the type of the array
        Parameters:
        a - the first array to combine
        b - the second array to combine
        writeTo - the array to combine the other two onto
        Returns:
        the combined array
      • range

        public static double[] range​(double stop)
        creates an double array that ranges from 0 to the specified end
        Parameters:
        stop - where to stop (exclusive)
        Returns:
        the array
      • range

        public static double[] range​(double start,
                                     double stop)
        creates an double array that ranges from a specified start to the specified end
        Parameters:
        start - where to start(inclusive)
        stop - where to stop (exclusive)
        Returns:
        the array
      • range

        public static double[] range​(double start,
                                     double stop,
                                     double step)
        creates an double array that ranges from a specified to the specified end with a specified step
        Parameters:
        start - where to start (inclusive)
        stop - where to stop (exclusive)
        step - the steps to take
        Returns:
        the array
      • range

        public static float[] range​(float stop)
        creates an double array that ranges from 0 to the specified end
        Parameters:
        stop - where to stop (exclusive)
        Returns:
        the array
      • range

        public static float[] range​(float start,
                                    float stop)
        creates an double array that ranges from a specified start to the specified end
        Parameters:
        start - where to start(inclusive)
        stop - where to stop (exclusive)
        Returns:
        the array
      • range

        public static float[] range​(float start,
                                    float stop,
                                    float step)
        creates an double array that ranges from a specified to the specified end with a specified step
        Parameters:
        start - where to start (inclusive)
        stop - where to stop (exclusive)
        step - the steps to take
        Returns:
        the array