Package com.jml.util

Class ArrayUtils

java.lang.Object
com.jml.util.ArrayUtils

public class ArrayUtils extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    asString​(double[] arr)
    Converts the array to a string.
    static String
    asString​(double[][] arr)
    Converts the array to a string.
    static String
    asString​(int[] arr)
    Converts the array to a string.
    static String
    asString​(int[][] arr)
    Converts the array to a string.
    static String
    asString​(Object[] arr)
    Converts the array to a string.
    static String
    asString2D​(Object[][] arr)
    Converts the array to a string.
    static int[]
    randomIndices​(int size)
    Generates an array of random unique integers.
    static double[][]
    round​(double[][] arr, int n)
    Rounds all numbers in array to n decimal places.
    static double[]
    round​(double[] arr, int n)
    Rounds all numbers in array to n decimal places.
    static double[]
    shuffle​(double[] arr)
    Randomly shuffles arrays using the Fisher–Yates algorithm.
    static double[][]
    shuffle​(double[]... arr)
    Randomly shuffles arrays using the Fisher–Yates algorithm.

    If more than one array is passed, the shuffled indices of all arrays will be the same.
    static double[][][]
    shuffle​(double[][]... arr)
    Randomly shuffles 2D arrays by rows using the Fisher–Yates algorithm.

    If more than one array is passed, the shuffled row indices of all arrays will be the same.
    static double[]
    toDouble​(String[] arr)
    Converts a String array to an array of doubles.
    Also see toDouble(String[][]) for 2D arrays.
    static double[][]
    toDouble​(String[][] arr)
    Converts a String array to an array of doubles.
    Also see toDouble(String[]) for 1D arrays.
    static double[]
    toDoubleFlat​(String[][] arr)
    Converts a String array to an 1D array of doubles.
    Also see toDouble(String[]) for 1D arrays.
    static int[]
    toInt​(double[] arr)
    Converts a double array to an int array.
    static int[]
    toIntFlat​(String[][] arr)
    Converts a String array to an 1D array of doubles.
    Also see toDouble(String[]) for 1D arrays.
    static Object[]
    toObject​(Object val)
    Converts an array to an Object array.
    static Object[][]
    Converts an array to an Object array.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ArrayUtils

      public ArrayUtils()
  • Method Details

    • round

      public static double[] round(double[] arr, int n)
      Rounds all numbers in array to n decimal places.
      Parameters:
      arr - Array to round.
      n - Number of digits to round to.
      Returns:
      Array with rounded numbers.
    • round

      public static double[][] round(double[][] arr, int n)
      Rounds all numbers in array to n decimal places.
      Parameters:
      arr - Array to round.
      n - Number of digits to round to.
      Returns:
      Array with rounded numbers.
    • asString

      public static String asString(int[] arr)
      Converts the array to a string.
      Parameters:
      arr - Array of interests.
      Returns:
      The string representation of arr.
    • asString

      public static String asString(int[][] arr)
      Converts the array to a string.
      Parameters:
      arr - Array of interests.
      Returns:
      The string representation of arr.
    • asString

      public static String asString(double[] arr)
      Converts the array to a string.
      Parameters:
      arr - Array of interests.
      Returns:
      The string representation of arr.
    • asString

      public static String asString(double[][] arr)
      Converts the array to a string.
      Parameters:
      arr - Array of interests.
      Returns:
      The string representation of arr.
    • asString

      public static String asString(Object[] arr)
      Converts the array to a string.
      Parameters:
      arr - Array of interests.
      Returns:
      The string representation of arr.
    • asString2D

      public static String asString2D(Object[][] arr)
      Converts the array to a string.
      Parameters:
      arr - Array of interests.
      Returns:
      The string representation of arr.
    • toObject

      public static Object[] toObject(Object val)
      Converts an array to an Object array.
      Returns:
      The array as an object array
    • toObject2D

      public static Object[][] toObject2D(Object val)
      Converts an array to an Object array.
      Returns:
      The array as an object array
    • toDouble

      public static double[] toDouble(String[] arr)
      Converts a String array to an array of doubles.
      Also see toDouble(String[][]) for 2D arrays.
      Parameters:
      arr - Array to convert to doubles.
      Returns:
      Content of arr in a double array.
    • toDouble

      public static double[][] toDouble(String[][] arr)
      Converts a String array to an array of doubles.
      Also see toDouble(String[]) for 1D arrays.
      Parameters:
      arr - Array to convert to doubles.
      Returns:
      Content of arr in a double array.
    • toDoubleFlat

      public static double[] toDoubleFlat(String[][] arr)
      Converts a String array to an 1D array of doubles.
      Also see toDouble(String[]) for 1D arrays.
      Parameters:
      arr - Array to convert to doubles.
      Returns:
      Content of arr in a 1D double array.
    • toIntFlat

      public static int[] toIntFlat(String[][] arr)
      Converts a String array to an 1D array of doubles.
      Also see toDouble(String[]) for 1D arrays.
      Parameters:
      arr - Array to convert to doubles.
      Returns:
      Content of arr in a 1D double array.
    • toInt

      public static int[] toInt(double[] arr)
      Converts a double array to an int array. Note, this is not the same as round(arr, 0) as this method does not round the double value just casts it and any precision after the decimal place is lost.
      Parameters:
      arr - Array to convert to int array.
      Returns:
      The given array converted to an int array.
    • shuffle

      public static double[] shuffle(double[] arr)
      Randomly shuffles arrays using the Fisher–Yates algorithm.
      Parameters:
      arr - Arrays to shuffle
      Returns:
      Arrays with rows randomly shuffled.
    • shuffle

      public static double[][] shuffle(double[]... arr)
      Randomly shuffles arrays using the Fisher–Yates algorithm.

      If more than one array is passed, the shuffled indices of all arrays will be the same. I.e. if the following arrays are passed:
           arr1: [1, 2, 3, 4, 5]
           arr2: [10, 20, 30, 40, 50]
      
           Then, if arr1 is shuffled to: [3, 1, 2, 5, 4]
           arr2 will be shuffled to: [30, 10, 20, 50, 40]
       
      Parameters:
      arr - Arrays to shuffle
      Returns:
      Arrays with rows randomly shuffled.
    • shuffle

      public static double[][][] shuffle(double[][]... arr)
      Randomly shuffles 2D arrays by rows using the Fisher–Yates algorithm.

      If more than one array is passed, the shuffled row indices of all arrays will be the same. I.e. if the following arrays are passed:
           arr1: [1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]
      
           arr2: [10, 20, 30],
                 [40, 50, 60],
                 [70, 80, 90]
      
           Then, if arr1 is shuffled to: [4, 5, 6],
                                         [1, 2, 3],
                                         [7, 8, 9]
      
           arr2 will be shuffled to: [40, 50, 60],
                                     [10, 20, 30],
                                     [70, 80, 90]
       
      Parameters:
      arr - 2D Arrays to shuffle
      Returns:
      Arrays with rows randomly shuffled.
    • randomIndices

      public static int[] randomIndices(int size)
      Generates an array of random unique integers.
      Returns:
      A list of random unique integers.