Package io.bhagat.util
Class ArrayUtil
- java.lang.Object
-
- io.bhagat.util.ArrayUtil
-
public final class ArrayUtil extends java.lang.ObjectA few of Utility methods for Arrays and ArrayLists- Author:
- Bhagat
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classArrayUtil.InvalidSizeExceptionAn 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 arraystatic <E> E[]copy(E[] arr, E[] arr2)Copies one array into anotherstatic <E> java.util.ArrayList<E>copy(java.util.ArrayList<E> list)Copies the list into a newArrayListat a different memory locationstatic <E> intindexOf(E[] arr, E obj)finds the index of a certain object in an arraystatic <E> E[]map(E[] arr, Function<E,E> function)maps a function to each element in an arraystatic <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 functionstatic <E> E[]newArrayFromArrayList(java.util.ArrayList<E> list, E[] arr)This method creates an array from anArrayListstatic java.util.ArrayList<java.lang.Boolean>newArrayList(boolean... items)This method creates a newArrayListwith the elements specified by the parameters passed into the methodstatic java.util.ArrayList<java.lang.Byte>newArrayList(byte... items)This method creates a newArrayListwith the elements specified by the parameters passed into the methodstatic java.util.ArrayList<java.lang.Character>newArrayList(char... items)This method creates a newArrayListwith the elements specified by the parameters passed into the methodstatic java.util.ArrayList<java.lang.Double>newArrayList(double... items)This method creates a newArrayListwith the elements specified by the parameters passed into the methodstatic java.util.ArrayList<java.lang.Float>newArrayList(float... items)This method creates a newArrayListwith the elements specified by the parameters passed into the methodstatic java.util.ArrayList<java.lang.Integer>newArrayList(int... items)This method creates a newArrayListwith the elements specified by the parameters passed into the methodstatic java.util.ArrayList<java.lang.Long>newArrayList(long... items)This method creates a newArrayListwith the elements specified by the parameters passed into the methodstatic java.util.ArrayList<java.lang.Short>newArrayList(short... items)This method creates a newArrayListwith the elements specified by the parameters passed into the methodstatic <E> java.util.ArrayList<E>newArrayList(E... items)This method creates a newArrayListwith the elements specified by the parameters passed into the methodstatic voidprintArr(boolean... arr)Prints out the boolean array in anArrayListformatstatic voidprintArr(byte... arr)Prints out the byte array in anArrayListformatstatic voidprintArr(char... arr)Prints out the char array in anArrayListformatstatic voidprintArr(double... arr)Prints out the double array in anArrayListformatstatic voidprintArr(float... arr)Prints out the float array in anArrayListformatstatic voidprintArr(int... arr)Prints out the int array in anArrayListformatstatic voidprintArr(long... arr)Prints out the long array in anArrayListformatstatic voidprintArr(short... arr)Prints out the short array in anArrayListformatstatic <E> voidprintArr(E... arr)Prints out an array in anArrayListformatstatic double[]range(double stop)creates an double array that ranges from 0 to the specified endstatic double[]range(double start, double stop)creates an double array that ranges from a specified start to the specified endstatic double[]range(double start, double stop, double step)creates an double array that ranges from a specified to the specified end with a specified stepstatic float[]range(float stop)creates an double array that ranges from 0 to the specified endstatic float[]range(float start, float stop)creates an double array that ranges from a specified start to the specified endstatic float[]range(float start, float stop, float step)creates an double array that ranges from a specified to the specified end with a specified stepstatic <E> E[]reshape(E[][] arr, E[] arr2)Reshapes a two dimensional array into a one dimensional arraystatic <E> E[][]reshape(E[] arr, E[][] arr2)Reshapes a one dimensional array into a two dimensional array
-
-
-
Method Detail
-
copy
public static <E> java.util.ArrayList<E> copy(java.util.ArrayList<E> list)
Copies the list into a newArrayListat 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 copiedarr2- the array to copy to- Returns:
- the copied array
-
reshape
public static <E> E[][] reshape(E[] arr, E[][] arr2) throws ArrayUtil.InvalidSizeExceptionReshapes a one dimensional array into a two dimensional array- Type Parameters:
E- the array type- Parameters:
arr- the 1D arrayarr2- 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.InvalidSizeExceptionReshapes a two dimensional array into a one dimensional array- Type Parameters:
E- the array type- Parameters:
arr- the 2D arrayarr2- 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 arrayfunction- 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 typeT- the final array type- Parameters:
arr- the original arrayfunction- the functionwriteTo- 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 arrayobj- 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 anArrayList- Type Parameters:
E- the array type- Parameters:
list- the array list to create the array fromarr- 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 newArrayListwith the elements specified by the parameters passed into the method- Type Parameters:
E- the array type- Parameters:
items- The elements that will fill theArrayListto be returned- Returns:
- A new
ArrayListwith the items specified from parameters
-
newArrayList
@SafeVarargs public static java.util.ArrayList<java.lang.Boolean> newArrayList(boolean... items)
This method creates a newArrayListwith the elements specified by the parameters passed into the method- Parameters:
items- The boolean elements that will fill theArrayListto be returned- Returns:
- A new
ArrayListwith the items specified from parameters
-
newArrayList
@SafeVarargs public static java.util.ArrayList<java.lang.Byte> newArrayList(byte... items)
This method creates a newArrayListwith the elements specified by the parameters passed into the method- Parameters:
items- The byte elements that will fill theArrayListto be returned- Returns:
- A new
ArrayListwith the items specified from parameters
-
newArrayList
@SafeVarargs public static java.util.ArrayList<java.lang.Character> newArrayList(char... items)
This method creates a newArrayListwith the elements specified by the parameters passed into the method- Parameters:
items- The char elements that will fill theArrayListto be returned- Returns:
- A new
ArrayListwith the items specified from parameters
-
newArrayList
@SafeVarargs public static java.util.ArrayList<java.lang.Short> newArrayList(short... items)
This method creates a newArrayListwith the elements specified by the parameters passed into the method- Parameters:
items- The short elements that will fill theArrayListto be returned- Returns:
- A new
ArrayListwith the items specified from parameters
-
newArrayList
@SafeVarargs public static java.util.ArrayList<java.lang.Integer> newArrayList(int... items)
This method creates a newArrayListwith the elements specified by the parameters passed into the method- Parameters:
items- The int elements that will fill theArrayListto be returned- Returns:
- A new
ArrayListwith the items specified from parameters
-
newArrayList
@SafeVarargs public static java.util.ArrayList<java.lang.Long> newArrayList(long... items)
This method creates a newArrayListwith the elements specified by the parameters passed into the method- Parameters:
items- The long elements that will fill theArrayListto be returned- Returns:
- A new
ArrayListwith the items specified from parameters
-
newArrayList
@SafeVarargs public static java.util.ArrayList<java.lang.Float> newArrayList(float... items)
This method creates a newArrayListwith the elements specified by the parameters passed into the method- Parameters:
items- The float elements that will fill theArrayListto be returned- Returns:
- A new
ArrayListwith the items specified from parameters
-
newArrayList
@SafeVarargs public static java.util.ArrayList<java.lang.Double> newArrayList(double... items)
This method creates a newArrayListwith the elements specified by the parameters passed into the method- Parameters:
items- The double elements that will fill theArrayListto be returned- Returns:
- A new
ArrayListwith the items specified from parameters
-
printArr
@SafeVarargs public static <E> void printArr(E... arr)
Prints out an array in anArrayListformat- 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 anArrayListformat- Parameters:
arr- Array to be printed
-
printArr
@SafeVarargs public static void printArr(byte... arr)
Prints out the byte array in anArrayListformat- Parameters:
arr- Array to be printed
-
printArr
@SafeVarargs public static void printArr(char... arr)
Prints out the char array in anArrayListformat- Parameters:
arr- Array to be printed
-
printArr
@SafeVarargs public static void printArr(short... arr)
Prints out the short array in anArrayListformat- Parameters:
arr- Array to be printed
-
printArr
@SafeVarargs public static void printArr(int... arr)
Prints out the int array in anArrayListformat- Parameters:
arr- Array to be printed
-
printArr
@SafeVarargs public static void printArr(long... arr)
Prints out the long array in anArrayListformat- Parameters:
arr- Array to be printed
-
printArr
@SafeVarargs public static void printArr(float... arr)
Prints out the float array in anArrayListformat- Parameters:
arr- Array to be printed
-
printArr
@SafeVarargs public static void printArr(double... arr)
Prints out the double array in anArrayListformat- 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 combineb- the second array to combinewriteTo- 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
-
-