Package jsl.utilities
Class JSLArrayUtil
- java.lang.Object
-
- jsl.utilities.JSLArrayUtil
-
public class JSLArrayUtil extends java.lang.Object
This class has some array manipulation methods that I have found useful over the years. Other libraries (e.g. guava and apache commons) also have array utilities which you might find useful.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static double[]
addConstant(double[] a, double c)
static double[]
addElements(double[] a, double[] b)
Adds the arrays element by element.static <T> java.lang.String[]
asStringArray(T[] array)
static boolean
compareArrays(double[] first, double[] second)
static double[][]
copy2DArray(double[][] src)
static int[][]
copy2DArray(int[][] src)
static double[]
copyOf(double[] src)
A convenience method for copying the entire arraystatic int[]
copyOf(int[] src)
A convenience method for copying the entire arraystatic double[][]
copyToColumns(java.util.LinkedHashMap<java.lang.String,double[]> labeledColumns)
Each labeled array in the map becomes a column in the returned array.static double[][]
copyToRows(java.util.LinkedHashMap<java.lang.String,double[]> labeledRows)
Each labeled array in the map becomes a row in the returned array, which may be ragged because each row in the array have a different length.static double[]
copyWithout(int index, double[] fromA)
Copies all but element index of array fromA into array toBstatic double[]
copyWithout(int index, double[] fromA, double[] toB)
Copies all but element index of array fromA into array toBstatic int
countElements(java.util.List objects, java.lang.Class targetClass)
Returns a count of the elements that are of the same type as the target class.static double[]
divideConstant(double[] a, double c)
static double[][]
expandToRectangular(double[][] array)
Copies the supplied array by expanding to the maximum number of columns of the supplied (potentially ragged) array so that the returned array is rectangular, where all row arrays have the same number of elements (columns).static double[][]
expandToRectangular(double[][] array, double fillValue)
Copies the supplied array by expanding to the maximum number of columns of the supplied (ragged) array so that the returned array is rectangular, where all row arrays have the same number of elements (columns).static void
fillColumn(int col, double[] src, double[][] dest)
The destination array is mutated by this methodstatic int
findIndex(double element, double[] array)
If the array is empty or null, -1 is returned.static int
findIndex(int element, int[] array)
If the array is empty or null, -1 is returned.static int
findIndex(java.lang.String element, java.lang.String[] array)
If the array is empty or null, -1 is returned.static double[]
getColumn(int k, double[][] matrix)
static int[]
getColumn(int k, int[][] matrix)
static java.lang.Object[]
getColumn(int index, java.lang.Object[][] matrix)
static <T> java.util.List<T>
getElements(java.util.List objects, java.lang.Class<T> targetClass)
Returns a list of the elements that are of the same type as the target class.static int
getIndexOfMax(double[] x)
Returns the index associated with the maximum element in the array For ties, this returns the first found.static int
getIndexOfMax(int[] x)
Returns the index associated with the maximum element in the array For ties, this returns the first foundstatic int
getIndexOfMax(long[] x)
Returns the index associated with the maximum element in the array For ties, this returns the first foundstatic int
getIndexOfMin(double[] x)
Returns the index associated with the minimum element in the array For ties, this returns the first found.static int
getIndexOfMin(int[] x)
Returns the index associated with the minimum element in the array For ties, this returns the first found.static int
getIndexOfMin(long[] x)
Returns the index associated with the minimum element in the array For ties, this returns the first foundstatic double
getMax(double[] x)
static int
getMax(int[] x)
static long
getMax(long[] x)
static int
getMaxNumColumns(double[][] array)
Assumes that the array can be ragged.static double
getMin(double[] x)
static int
getMin(int[] x)
static long
getMin(long[] x)
static double[]
getMinMaxScaledArray(double[] array)
Returns a new array that has been scaled so that the values are between the minimum and maximum values of the supplied arraystatic int
getMinNumColumns(double[][] array)
Assumes that the array can be ragged.static double
getRange(double[] array)
static double
getSumSquareRoots(double[] array)
The array must have non-negative elements and not be nullstatic double
getSumSquares(double[] array)
The array must not be nullstatic boolean
isRectangular(double[][] array)
An array is rectangular if all rows have the same number of elements (columns).static boolean
isRectangular(int[][] array)
An array is rectangular if all rows have the same number of elements (columns).static <T> boolean
isRectangular(T[][] array)
An array is rectangular if all rows have the same number of elements (columns).static double[]
multiplyConstant(double[] a, double c)
static double[]
multiplyElements(double[] a, double[] b)
Multiplies the arrays element by element.static double[]
parseDouble(java.lang.String[] dblStrings)
If the string cannot be converted to a number then the array value is assigned Double.NaNstatic double[][]
parseTo2DArray(java.util.List<java.lang.String[]> entries)
Assumes that the entries in the list are string representations of double values.static double[]
scanToArray(java.nio.file.Path pathToFile)
Assumes that the file holds doubles with each value on a different line 1.0 4.0 2.0 etchstatic double[]
subtractConstant(double[] a, double c)
static java.lang.String
toCSVString(double[] array)
static java.lang.String
toCSVString(int[] array)
static java.lang.String
toCSVString(long[] array)
static java.lang.Double[]
toDouble(double[] array)
Convert the array of double to an array of Double with each element the corresponding valuestatic java.lang.Double[][]
toDouble(double[][] array)
Convert the 2D array of double to a 2D array of Double with each element the corresponding valuestatic double[]
toDouble(int[] array)
Convert the array of int to an array of double with each element the corresponding valuestatic double[]
toDouble(long[] array)
Convert the array of long to an array of double with each element the corresponding valuestatic double[]
toDouble(java.lang.Integer[] array)
Convert the array of int to an array of double with each element the corresponding valuestatic double[]
toDouble(java.lang.Long[] array)
Convert the array of long to an array of double with each element the corresponding valuestatic java.lang.Integer[]
toInteger(int[] array)
Convert the array of int to an array of Intger with each element the corresponding valuestatic java.lang.Integer[][]
toInteger(int[][] array)
Convert the 2D array of int to a 2D array of Integer with each element the corresponding valuestatic double[]
toPrimitive(java.lang.Double[] array)
Converts any null values to zerostatic int[]
toPrimitive(java.lang.Integer[] array)
Converts any null values to zerostatic long[]
toPrimitive(java.lang.Long[] array)
Converts any null values to zerostatic double[]
toPrimitiveDouble(java.util.List<java.lang.Double> doubleList)
Converts any nulls to zerostatic int[]
toPrimitiveInteger(java.util.List<java.lang.Integer> List)
Converts any nulls to zerostatic long[]
toPrimitiveLong(java.util.List<java.lang.Long> List)
Converts any nulls to zerostatic java.lang.String[]
toString(double[] array)
Convert the array of double to an array of strings with each element the corresponding valuestatic double[][]
transpose2DArray(double[][] array)
Transposes the array returned transpose[x][y] = array[y][x]static int[][]
transpose2DArray(int[][] array)
Transposes the array returned transpose[x][y] = array[y][x]static double[][]
trimToRectangular(double[][] array)
Copies the supplied array by trimming to the minimum number of columns of the supplied (potentially ragged) array so that the returned array is rectangular, where all row arrays have the same number of elements (columns)
-
-
-
Method Detail
-
getIndexOfMin
public static int getIndexOfMin(double[] x)
Returns the index associated with the minimum element in the array For ties, this returns the first found.- Parameters:
x
- the array to search, must not be null or empty- Returns:
- the index associated with the minimum element
-
getMin
public static double getMin(double[] x)
- Parameters:
x
- the array to search, must not be null or empty- Returns:
- the minimum value in the array
-
getIndexOfMax
public static int getIndexOfMax(double[] x)
Returns the index associated with the maximum element in the array For ties, this returns the first found.- Parameters:
x
- the array to search, must not be null or empty- Returns:
- the index associated with the maximum element
-
getMax
public static double getMax(double[] x)
- Parameters:
x
- the array to search, must not be null or empty- Returns:
- the maximum value in the array
-
getIndexOfMin
public static int getIndexOfMin(int[] x)
Returns the index associated with the minimum element in the array For ties, this returns the first found.- Parameters:
x
- the array to search, must not be null or empty- Returns:
- the index associated with the minimum element
-
getMin
public static int getMin(int[] x)
- Parameters:
x
- the array to search, must not be null or empty- Returns:
- the minimum value in the array
-
getIndexOfMax
public static int getIndexOfMax(int[] x)
Returns the index associated with the maximum element in the array For ties, this returns the first found- Parameters:
x
- the array to search, must not be null or empty- Returns:
- the index associated with the maximum element
-
getMax
public static int getMax(int[] x)
- Parameters:
x
- the array to search, must not be null or empty- Returns:
- the maximum value in the array
-
getIndexOfMin
public static int getIndexOfMin(long[] x)
Returns the index associated with the minimum element in the array For ties, this returns the first found- Parameters:
x
- the array to search, must not be null or empty- Returns:
- the index associated with the minimum element
-
getMin
public static long getMin(long[] x)
- Parameters:
x
- the array to search, must not be null or empty- Returns:
- the minimum value in the array
-
getIndexOfMax
public static int getIndexOfMax(long[] x)
Returns the index associated with the maximum element in the array For ties, this returns the first found- Parameters:
x
- the array to search, must not be null or empty- Returns:
- the index associated with the maximum element
-
getMax
public static long getMax(long[] x)
- Parameters:
x
- the array to search, must not be null or empty- Returns:
- the maximum value in the array
-
getRange
public static double getRange(double[] array)
- Parameters:
array
- the array to operate on- Returns:
- getMax() - getMin()
-
findIndex
public static int findIndex(int element, int[] array)
If the array is empty or null, -1 is returned.- Parameters:
element
- the element to search forarray
- the array to search in- Returns:
- the index of the first occurrence of the element
-
findIndex
public static int findIndex(double element, double[] array)
If the array is empty or null, -1 is returned.- Parameters:
element
- the element to search forarray
- the array to search in- Returns:
- the index of the first occurrence of the element
-
findIndex
public static int findIndex(java.lang.String element, java.lang.String[] array)
If the array is empty or null, -1 is returned.- Parameters:
element
- the element to search forarray
- the array to search in- Returns:
- the index of the first occurrence of the element
-
getMinMaxScaledArray
public static double[] getMinMaxScaledArray(double[] array)
Returns a new array that has been scaled so that the values are between the minimum and maximum values of the supplied array- Parameters:
array
- the array to scale, must not be null- Returns:
- the scaled array
-
copyWithout
public static double[] copyWithout(int index, double[] fromA)
Copies all but element index of array fromA into array toB- Parameters:
index
- index of element to leave out, must be 0 to fromA.length-1fromA
- array to copy from, must not be null- Returns:
- a reference to the array toB
-
copyWithout
public static double[] copyWithout(int index, double[] fromA, double[] toB)
Copies all but element index of array fromA into array toB- Parameters:
index
- index of element to leave out, must be 0 to fromA.length-1fromA
- array to copy from, must not be nulltoB
- array to copy to, must be length fromA.length - 1- Returns:
- a reference to the array toB
-
addConstant
public static double[] addConstant(double[] a, double c)
- Parameters:
a
- the array to add the constant toc
- the constant to add to each element- Returns:
- the transformed array
-
subtractConstant
public static double[] subtractConstant(double[] a, double c)
- Parameters:
a
- the array to add the constant toc
- the constant to subtract from each element- Returns:
- the transformed array
-
multiplyConstant
public static double[] multiplyConstant(double[] a, double c)
- Parameters:
a
- the array to multiply the constant byc
- the constant to multiply against each element- Returns:
- the transformed array
-
divideConstant
public static double[] divideConstant(double[] a, double c)
- Parameters:
a
- the array to divide the constant byc
- the constant to divide each element, cannot be zero- Returns:
- the transformed array
-
multiplyElements
public static double[] multiplyElements(double[] a, double[] b)
Multiplies the arrays element by element. Arrays must have same length and must not be null.- Parameters:
a
- the first arrayb
- the second array- Returns:
- the array containing a[i]*b[i]
-
getMinNumColumns
public static int getMinNumColumns(double[][] array)
Assumes that the array can be ragged. Returns the number of columns necessary that would cause the array to not be ragged. In other words, the minimum number of columns to make the array a unragged array (matrix) where all row arrays have the same number of elements.- Parameters:
array
- the array to check, must not be null- Returns:
- the minimum number of columns in the array
-
trimToRectangular
public static double[][] trimToRectangular(double[][] array)
Copies the supplied array by trimming to the minimum number of columns of the supplied (potentially ragged) array so that the returned array is rectangular, where all row arrays have the same number of elements (columns)- Parameters:
array
- the array to copy- Returns:
- the copy
-
expandToRectangular
public static double[][] expandToRectangular(double[][] array)
Copies the supplied array by expanding to the maximum number of columns of the supplied (potentially ragged) array so that the returned array is rectangular, where all row arrays have the same number of elements (columns).The expanded elements will be filled with 0.0
- Parameters:
array
- the array to copy- Returns:
- the copy
-
expandToRectangular
public static double[][] expandToRectangular(double[][] array, double fillValue)
Copies the supplied array by expanding to the maximum number of columns of the supplied (ragged) array so that the returned array is rectangular, where all row arrays have the same number of elements (columns).The expanded elements will be filled with the supplied fill value
- Parameters:
array
- the array to copy- Returns:
- the copy
-
isRectangular
public static <T> boolean isRectangular(T[][] array)
An array is rectangular if all rows have the same number of elements (columns).- Parameters:
array
- the array to check- Returns:
- true if the array is rectangular
-
isRectangular
public static boolean isRectangular(double[][] array)
An array is rectangular if all rows have the same number of elements (columns).- Parameters:
array
- the array to check- Returns:
- true if the array is rectangular
-
isRectangular
public static boolean isRectangular(int[][] array)
An array is rectangular if all rows have the same number of elements (columns).- Parameters:
array
- the array to check- Returns:
- true if the array is rectangular
-
getMaxNumColumns
public static int getMaxNumColumns(double[][] array)
Assumes that the array can be ragged. Returns the number of elements in the row array that has the most elements.- Parameters:
array
- the array to check, must not be null- Returns:
- the minimum number of columns in the array
-
getColumn
public static double[] getColumn(int k, double[][] matrix)
- Parameters:
k
- the kth column to be extracted (zero based indexing)matrix
- must not be null, assumed 2D rectangular array (i.e. all rows have the same number of columns)- Returns:
- a copy of the extracted column
-
getColumn
public static int[] getColumn(int k, int[][] matrix)
- Parameters:
k
- the kth column to be extracted (zero based indexing)matrix
- must not be null, assumed 2D rectangular array (i.e. all rows have the same number of columns)- Returns:
- a copy of the extracted column
-
getColumn
public static java.lang.Object[] getColumn(int index, java.lang.Object[][] matrix)
- Parameters:
index
- the column to be extracted (zero based indexing)matrix
- must not be null, assumed 2D rectangular array (i.e. all rows have the same number of columns)- Returns:
- a copy of the extracted column
-
copyOf
public static double[] copyOf(double[] src)
A convenience method for copying the entire array- Parameters:
src
- the array to copy- Returns:
- the copy
-
copyOf
public static int[] copyOf(int[] src)
A convenience method for copying the entire array- Parameters:
src
- the array to copy- Returns:
- the copy
-
copy2DArray
public static double[][] copy2DArray(double[][] src)
- Parameters:
src
- the source array to copy- Returns:
- a copy of the array
-
copy2DArray
public static int[][] copy2DArray(int[][] src)
- Parameters:
src
- the source array to copy- Returns:
- a copy of the array
-
fillColumn
public static void fillColumn(int col, double[] src, double[][] dest)
The destination array is mutated by this method- Parameters:
col
- the column in the destination to fillsrc
- the source for filling the column, must not be nulldest
- the destination array, assumed to be rectangular, must not be null
-
getSumSquares
public static double getSumSquares(double[] array)
The array must not be null- Parameters:
array
- the input array- Returns:
- the sum of the squares of the elements of the array
-
getSumSquareRoots
public static double getSumSquareRoots(double[] array)
The array must have non-negative elements and not be null- Parameters:
array
- the input array- Returns:
- the sum of the square roots of the elements of the array
-
addElements
public static double[] addElements(double[] a, double[] b)
Adds the arrays element by element. Arrays must have same length and must not be null.- Parameters:
a
- the first arrayb
- the second array- Returns:
- the array containing a[i]+b[i]
-
getElements
public static <T> java.util.List<T> getElements(java.util.List objects, java.lang.Class<T> targetClass)
Returns a list of the elements that are of the same type as the target class. Usage: getElements(objects, String.class);- Type Parameters:
T
- the type of the element to search for- Parameters:
objects
- the list that can hold anythingtargetClass
- the class type to find in the list, should be same as T- Returns:
- a list that holds the items of the targetClass
-
countElements
public static int countElements(java.util.List objects, java.lang.Class targetClass)
Returns a count of the elements that are of the same type as the target class.- Parameters:
objects
- the list that can hold anythingtargetClass
- the class type to find in the list, should be same as T- Returns:
- a list that holds the items of the targetClass
-
compareArrays
public static boolean compareArrays(double[] first, double[] second)
- Parameters:
first
- the first arraysecond
- the second array- Returns:
- true if all elements are equal
-
toPrimitive
public static double[] toPrimitive(java.lang.Double[] array)
Converts any null values to zero- Parameters:
array
- the array to copy- Returns:
- the primitive array
-
toPrimitiveDouble
public static double[] toPrimitiveDouble(java.util.List<java.lang.Double> doubleList)
Converts any nulls to zero- Parameters:
doubleList
- the list to convert- Returns:
- the primitive array
-
toPrimitive
public static int[] toPrimitive(java.lang.Integer[] array)
Converts any null values to zero- Parameters:
array
- the array to copy- Returns:
- the primitive array
-
toPrimitiveInteger
public static int[] toPrimitiveInteger(java.util.List<java.lang.Integer> List)
Converts any nulls to zero- Parameters:
List
- the list to convert- Returns:
- the primitive array
-
toPrimitive
public static long[] toPrimitive(java.lang.Long[] array)
Converts any null values to zero- Parameters:
array
- the array to copy- Returns:
- the primitive array
-
toPrimitiveLong
public static long[] toPrimitiveLong(java.util.List<java.lang.Long> List)
Converts any nulls to zero- Parameters:
List
- the list to convert- Returns:
- the primitive array
-
toString
public static java.lang.String[] toString(double[] array)
Convert the array of double to an array of strings with each element the corresponding value- Parameters:
array
- the array of doubles- Returns:
- the array of strings representing the values of the doubles
-
toCSVString
public static java.lang.String toCSVString(double[] array)
- Parameters:
array
- the array to convert- Returns:
- a comma delimited string of the array, if empty or null, returns the empty string
-
toCSVString
public static java.lang.String toCSVString(int[] array)
- Parameters:
array
- the array to convert- Returns:
- a comma delimited string of the array, if empty or null, returns the empty string
-
toCSVString
public static java.lang.String toCSVString(long[] array)
- Parameters:
array
- the array to convert- Returns:
- a comma delimited string of the array, if empty or null, returns the empty string
-
toDouble
public static java.lang.Double[] toDouble(double[] array)
Convert the array of double to an array of Double with each element the corresponding value- Parameters:
array
- the array of doubles- Returns:
- the array of Doubles representing the values of the doubles
-
toDouble
public static double[] toDouble(int[] array)
Convert the array of int to an array of double with each element the corresponding value- Parameters:
array
- the array of ints- Returns:
- the array of doubles representing the values of the ints
-
toDouble
public static double[] toDouble(java.lang.Integer[] array)
Convert the array of int to an array of double with each element the corresponding value- Parameters:
array
- the array of ints- Returns:
- the array of doubles representing the values of the ints
-
toDouble
public static double[] toDouble(long[] array)
Convert the array of long to an array of double with each element the corresponding value- Parameters:
array
- the array of longs- Returns:
- the array of doubles representing the values of the longs
-
toDouble
public static double[] toDouble(java.lang.Long[] array)
Convert the array of long to an array of double with each element the corresponding value- Parameters:
array
- the array of longs- Returns:
- the array of doubles representing the values of the longs
-
toDouble
public static java.lang.Double[][] toDouble(double[][] array)
Convert the 2D array of double to a 2D array of Double with each element the corresponding value- Parameters:
array
- the array of doubles- Returns:
- the array of Doubles representing the values of the doubles
-
toInteger
public static java.lang.Integer[] toInteger(int[] array)
Convert the array of int to an array of Intger with each element the corresponding value- Parameters:
array
- the array of ints- Returns:
- the array of Integers representing the values of the ints
-
toInteger
public static java.lang.Integer[][] toInteger(int[][] array)
Convert the 2D array of int to a 2D array of Integer with each element the corresponding value- Parameters:
array
- the array of int- Returns:
- the array of Integer representing the values of the int
-
parseDouble
public static double[] parseDouble(java.lang.String[] dblStrings)
If the string cannot be converted to a number then the array value is assigned Double.NaN- Parameters:
dblStrings
- an array of strings that represent doubles- Returns:
- the parsed doubles as an array
-
transpose2DArray
public static int[][] transpose2DArray(int[][] array)
Transposes the array returned transpose[x][y] = array[y][x]- Parameters:
array
- an array with m rows and n columns- Returns:
- an array with n columns and m rows
-
transpose2DArray
public static double[][] transpose2DArray(double[][] array)
Transposes the array returned transpose[x][y] = array[y][x]- Parameters:
array
- an array with m rows and n columns- Returns:
- an array with n columns and m rows
-
copyToRows
public static double[][] copyToRows(java.util.LinkedHashMap<java.lang.String,double[]> labeledRows)
Each labeled array in the map becomes a row in the returned array, which may be ragged because each row in the array have a different length.- Parameters:
labeledRows
- a map holding named rows of data- Returns:
- a 2D array, where rows of the array hold the data in the order returned from the string labels.
-
copyToColumns
public static double[][] copyToColumns(java.util.LinkedHashMap<java.lang.String,double[]> labeledColumns)
Each labeled array in the map becomes a column in the returned array. Each array in the map must have the same number of elements.- Parameters:
labeledColumns
- a map holding named columns of data- Returns:
- a 2D array, where columns of the array hold the data in the order returned from the string labels.
-
parseTo2DArray
public static double[][] parseTo2DArray(java.util.List<java.lang.String[]> entries)
Assumes that the entries in the list are string representations of double values. Each String[] can have a different number of elements. Thus, the returned array may be ragged.- Parameters:
entries
- the list of data entries- Returns:
- the 2D array
-
scanToArray
public static double[] scanToArray(java.nio.file.Path pathToFile)
Assumes that the file holds doubles with each value on a different line 1.0 4.0 2.0 etch- Parameters:
pathToFile
- the path to a file holding the data- Returns:
- the data as an array
-
asStringArray
public static <T> java.lang.String[] asStringArray(T[] array)
- Type Parameters:
T
- the type of the objects- Parameters:
array
- the array of objects- Returns:
- a String array holding the string value of the elements of the array
-
-