Class ArrayUtils

java.lang.Object
jmt.framework.data.ArrayUtils

public class ArrayUtils extends Object
Assorted array mangling stuff.
  • resize methods return a resized copy of an array, preserving data where possible and padding with a specified value
  • copy methods perform a deep copy of an array. They come in two flavors: one that copies into an existing array (that must have been properly initialized) and one that creates the copy from scratch
  • delete methods delete an element in an array, returning a reduced-size copy in which following elements have been shifted to fill the gap. For multidim arrays shallow copy is performed when possible - ie do not assume the new array to be independent from the old one
  • toCSV and fromCSV methods input/output single rows as Strings of comma-separated values.
  • toString methods return a somewhat useful String representation of an array.

NOTE: fromCSV uses the new regex String splitting methods introduced in 1.4. For use in pre-1.4 JDKs it must be rewritten using StringTokenizer.

  • Method Summary

    Modifier and Type
    Method
    Description
    static double[]
    copy(double[] array)
     
    static void
    copy(double[] from, double[] to)
     
    static int[]
    copy(int[] array)
     
    static void
    copy(int[] from, int[] to)
     
    static String[]
    copy(String[] array)
     
    static void
    copy(String[] from, String[] to)
     
    static double[][]
    copy2(double[][] array)
     
    static void
    copy2(double[][] from, double[][] to)
     
    static void
    copy2to3(double[][] source, double[][][] dest, int k)
    Copies a 2D array in a 3D one.
    static double[][][]
    copy3(double[][][] array)
     
    static void
    copy3(double[][][] from, double[][][] to)
     
    static double[][][]
    copy3per2(double[][][] array3, double[][] factor)
     
    static double[]
    delete(double[] arr, int idx)
     
    static int[]
    delete(int[] arr, int idx)
     
    static String[]
    delete(String[] arr, int idx)
     
    static double[][]
    delete2_1(double[][] arr, int idx)
     
    static double[][]
    delete2_2(double[][] arr, int idx)
     
    static double[][][]
    delete3_1(double[][][] arr, int idx)
     
    static double[][][]
    delete3_2(double[][][] arr, int idx)
     
    static double[][][]
    delete3_3(double[][][] arr, int idx)
     
    static boolean
    equals2(double[][] arr1, double[][] arr2)
     
    static boolean
    equals3(double[][][] arr1, double[][][] arr2)
     
    static double[]
    extract1(double[][] array, int index2)
    for each i result[i] = array[i][index2]
    static double[][]
    extract13(double[][][] array, int index2)
    for each i,j result[i][j] = array[i][index2][j-1]
    static boolean
    fromCSV(double[] array, String str)
     
    static double[]
    Extract a double[] given a string separated by ';'
    static boolean
    fromCSV3_2(double[][][] array, int index1, int index3, String str)
     
    static void
    insert1(double[][] result, double[] array, int index2)
    for each i result[i][index2] = array[i]
    static double[][]
    makeFilled(int len1, int len2, double val)
    create an empty double[len1][len2]
    static double[]
    multiply(double[] source, double num)
    Given an array A and a number n returns the product A*n
    Author: Bertoli Marco
    static double[]
    prepend0(double[] array)
    array[n]->result[n+1], where result[0]=0 and for i=(0..n-1) result[i]=array[i-1]
    static double[]
    resize(double[] array, int newsize, double padding)
    Resizes an array, preserving data and padding as needed
    static int[]
    resize(int[] array, int newsize, int padding)
    Resizes an array, preserving data and padding as needed
    static String[]
    resize(String[] array, int newsize, String padding)
    Resizes an array, preserving data and padding as needed
    static <T> T[]
    resize(T[] array, int newsize)
    Resizes an array, preserving data and padding as needed
    static double[][]
    resize2(double[][] array, int newsize1, int newsize2, double padding)
    Resizes a 2-dimensional array preserving data and padding as needed
    static double[][][]
    resize3var(double[][][] array, int newsize1, int newsize2, int[] newsize3, double padding)
    Resizes a 3-dimensional array with variable sizes preserving data and padding as needed
    static String
    toCSV(double[] array)
     
    static String
    toCSV3_2(double[][][] array, int index1, int index3)
     
    static int[]
    toInt(double[] arr)
    for each i result[i]=(int)array[i]
    static String
    toString(double[] array)
     
    static String
    toString(int[] array)
     
    static String
    toString(Object[] array)
     
    static String
    toString2(double[][] array)
     
    static String
    toString3(double[][][] array)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • resize

      public static int[] resize(int[] array, int newsize, int padding)
      Resizes an array, preserving data and padding as needed
    • resize

      public static <T> T[] resize(T[] array, int newsize)
      Resizes an array, preserving data and padding as needed
    • resize

      public static double[] resize(double[] array, int newsize, double padding)
      Resizes an array, preserving data and padding as needed
    • resize

      public static String[] resize(String[] array, int newsize, String padding)
      Resizes an array, preserving data and padding as needed
    • resize2

      public static double[][] resize2(double[][] array, int newsize1, int newsize2, double padding)
      Resizes a 2-dimensional array preserving data and padding as needed
    • resize3var

      public static double[][][] resize3var(double[][][] array, int newsize1, int newsize2, int[] newsize3, double padding)
      Resizes a 3-dimensional array with variable sizes preserving data and padding as needed
    • copy

      public static String[] copy(String[] array)
    • copy

      public static double[] copy(double[] array)
    • copy

      public static int[] copy(int[] array)
    • copy2

      public static double[][] copy2(double[][] array)
      Returns:
      a deep copy of array
    • copy3

      public static double[][][] copy3(double[][][] array)
      Returns:
      a deep copy of array
    • copy3per2

      public static double[][][] copy3per2(double[][][] array3, double[][] factor)
    • copy

      public static void copy(String[] from, String[] to)
    • copy

      public static void copy(double[] from, double[] to)
    • copy

      public static void copy(int[] from, int[] to)
    • copy2

      public static void copy2(double[][] from, double[][] to)
    • copy3

      public static void copy3(double[][][] from, double[][][] to)
    • delete

      public static String[] delete(String[] arr, int idx)
    • delete

      public static int[] delete(int[] arr, int idx)
    • delete

      public static double[] delete(double[] arr, int idx)
    • delete2_1

      public static double[][] delete2_1(double[][] arr, int idx)
    • delete2_2

      public static double[][] delete2_2(double[][] arr, int idx)
    • delete3_1

      public static double[][][] delete3_1(double[][][] arr, int idx)
    • delete3_2

      public static double[][][] delete3_2(double[][][] arr, int idx)
    • delete3_3

      public static double[][][] delete3_3(double[][][] arr, int idx)
    • extract13

      public static double[][] extract13(double[][][] array, int index2)
      for each i,j result[i][j] = array[i][index2][j-1]
    • extract1

      public static double[] extract1(double[][] array, int index2)
      for each i result[i] = array[i][index2]
    • makeFilled

      public static double[][] makeFilled(int len1, int len2, double val)
      create an empty double[len1][len2]
    • insert1

      public static void insert1(double[][] result, double[] array, int index2)
      for each i result[i][index2] = array[i]
    • toInt

      public static int[] toInt(double[] arr)
      for each i result[i]=(int)array[i]
    • toCSV

      public static String toCSV(double[] array)
    • toCSV3_2

      public static String toCSV3_2(double[][][] array, int index1, int index3)
    • fromCSV

      public static boolean fromCSV(double[] array, String str)
    • fromCSV

      public static double[] fromCSV(String str)
      Extract a double[] given a string separated by ';'
      Parameters:
      str - input string
      Returns:
      double array
      Author: Bertoli Marco
    • fromCSV3_2

      public static boolean fromCSV3_2(double[][][] array, int index1, int index3, String str)
    • equals2

      public static boolean equals2(double[][] arr1, double[][] arr2)
    • equals3

      public static boolean equals3(double[][][] arr1, double[][][] arr2)
    • toString

      public static String toString(Object[] array)
    • toString

      public static String toString(int[] array)
    • toString

      public static String toString(double[] array)
    • toString2

      public static String toString2(double[][] array)
    • toString3

      public static String toString3(double[][][] array)
    • prepend0

      public static double[] prepend0(double[] array)
      array[n]->result[n+1], where result[0]=0 and for i=(0..n-1) result[i]=array[i-1]
    • copy2to3

      public static void copy2to3(double[][] source, double[][][] dest, int k)
      Copies a 2D array in a 3D one. Given source[i][j] elements are copied in position dest[i][j][k] where k is a given index.
      Author: Bertoli Marco
      Parameters:
      source - source 2D array
      dest - destination 3D array
      k - third coordinate to be left unchanged
    • multiply

      public static double[] multiply(double[] source, double num)
      Given an array A and a number n returns the product A*n
      Author: Bertoli Marco
      Parameters:
      source - source array A
      num - number to be multiplied n
      Returns:
      a new array with multiplied value