Class ArrayConversions

java.lang.Object
org.flag4j.util.ArrayConversions

public final class ArrayConversions extends Object

The ArrayConversions class provides utility methods for converting between various array types and formats. This includes conversions from primitive arrays to object arrays and between lists and arrays.

Designed to handle common conversion use cases efficiently, this class supports operations such as:

  • Converting primitive (or boxed primitive) arrays to Complex128 and Complex64 representations.
  • Boxing and unboxing of primitive and object types (e.g., int[] to Integer[]).
  • Converting arrays to ArrayList and vice versa.
  • Transforming between primitive and custom numerical representations (e.g., int[] to double[]).

Usage Examples


 // Convert an array of integers to Complex128 array.
 int[] intArray = {1, 2, 3};
 Complex128[] complexArray = ArrayConversions.toComplex128(intArray, null);

 // Convert an ArrayList of Integers to an int array.
 List<Integer> integerList = List.of(1, 2, 3);
 int[] intArrayFromList = ArrayConversions.fromIntegerList(integerList);

 // Convert a double array to an ArrayList.
 double[] doubleArray = {1.1, 2.2, 3.3};
 ArrayList<Double> doubleList = ArrayConversions.toArrayList(doubleArray);

 // Box a primitive int array to Integer[].
 int[] primitiveIntArray = {1, 2, 3};
 Integer[] boxedArray = ArrayConversions.boxed(primitiveIntArray);
 

Restrictions:

  • All source arrays and lists must be non-null unless explicitly stated otherwise.
  • The caller must ensure that destination arrays have sufficient capacity when provided.

Note: This class is a utility class and cannot be instantiated.

  • Method Details

    • toComplex128

      public static Complex128[] toComplex128(int[] src, Complex128[] dest)
      Converts array to an array of complex numbers.
      Parameters:
      src - Array to convert.
      dest - Destination array. If the destination array is null, a new array will be created.
      Returns:
      A reference to the dest array.
      Throws:
      IllegalArgumentException - If source and destination arrays do not have the same length.
    • toComplex128

      public static Complex128[] toComplex128(double[] src, Complex128[] dest)
      Converts array to an array of complex numbers.
      Parameters:
      src - Array to convert.
      dest - Destination array. If the destination array is null, a new array will be created.
      Returns:
      A reference to the dest array.
      Throws:
      IllegalArgumentException - If source and destination arrays do not have the same length.
    • toComplex128

      public static Complex128[] toComplex128(Integer[] src, Complex128[] dest)
      Converts array to an array of complex numbers.
      Parameters:
      src - Array to convert.
      dest - Destination array. If the destination array is null, a new array will be created.
      Returns:
      A reference to the dest array.
      Throws:
      IllegalArgumentException - If source and destination arrays do not have the same length.
    • toComplex128

      public static Complex128[] toComplex128(Double[] src, Complex128[] dest)
      Converts array to an array of complex numbers.
      Parameters:
      src - Array to convert.
      dest - Destination array. If the destination array is null, a new array will be created.
      Returns:
      A reference to the dest array.
      Throws:
      IllegalArgumentException - If source and destination arrays do not have the same length.
    • toComplex128

      public static Complex128[] toComplex128(Complex64[] src, Complex128[] dest)
      Converts an array to an array of complex numbers.
      Parameters:
      src - Array to convert.
      dest - Destination array. If the destination array is null, a new array will be created.
      Returns:
      A reference to the dest array.
      Throws:
      IllegalArgumentException - If source and destination arrays do not have the same length.
    • toComplex128

      public static Complex128[] toComplex128(String[] src, Complex128[] dest)
      Converts array to an array of complex numbers.
      Parameters:
      src - Array to convert.
      dest - Destination array. If the destination array is null, a new array will be created.
      Returns:
      A reference to the dest array.
      Throws:
      IllegalArgumentException - If source and destination arrays do not have the same length.
    • toComplex64

      public static Complex64[] toComplex64(int[] src, Complex64[] dest)
      Converts array to an array of complex numbers.
      Parameters:
      src - Array to convert.
      dest - Destination array. If the destination array is null, a new array will be created.
      Returns:
      A reference to the dest array.
      Throws:
      IllegalArgumentException - If source and destination arrays do not have the same length.
    • toComplex64

      public static Complex64[] toComplex64(float[] src, Complex64[] dest)
      Converts array to an array of complex numbers.
      Parameters:
      src - Array to convert.
      dest - Destination array. If the destination array is null, a new array will be created.
      Returns:
      A reference to the dest array.
      Throws:
      IllegalArgumentException - If source and destination arrays do not have the same length.
    • toComplex64

      public static Complex64[] toComplex64(Integer[] src, Complex64[] dest)
      Converts array to an array of complex numbers.
      Parameters:
      src - Array to convert.
      dest - Destination array. If the destination array is null, a new array will be created.
      Returns:
      A reference to the dest array.
      Throws:
      IllegalArgumentException - If source and destination arrays do not have the same length.
    • toComplex64

      public static Complex64[] toComplex64(Float[] src, Complex64[] dest)
      Converts array to an array of complex numbers.
      Parameters:
      src - Array to convert.
      dest - Destination array. If the destination array is null, a new array will be created.
      Returns:
      A reference to the dest array.
      Throws:
      IllegalArgumentException - If source and destination arrays do not have the same length.
    • toComplex64

      public static Complex64[] toComplex64(String[] src, Complex64[] dest)
      Converts array to an array of complex numbers.
      Parameters:
      src - Array to convert.
      dest - Destination array. If the destination array is null, a new array will be created.
      Returns:
      A reference to the dest array.
      Throws:
      IllegalArgumentException - If source and destination arrays do not have the same length.
    • toArrayList

      public static ArrayList<Double> toArrayList(double[] src)
      Converts an array of doubles to an array list.
      Parameters:
      src - Array to convert.
      Returns:
      An equivalent array list.
    • toArrayList

      public static ArrayList<Complex128> toArrayList(Complex128[] src)
      Converts an array of complex numbers to an array list.
      Parameters:
      src - Array to convert.
      Returns:
      An equivalent array list.
    • toComplexArrayList

      public static ArrayList<Complex128> toComplexArrayList(double[] src)
      Converts an array of doubles to a complex array list.
      Parameters:
      src - Array to convert.
      Returns:
      An equivalent complex array list.
    • toArrayList

      public static ArrayList<Integer> toArrayList(int[] src)
      Converts an array of doubles to an array list.
      Parameters:
      src - Array to convert.
      Returns:
      An equivalent array list.
    • fromDoubleList

      public static double[] fromDoubleList(List<Double> src)
      Converts a list of Doubles objects to a primitive array.
      Parameters:
      src - Source list to convert.
      Returns:
      An array containing the same values as the src list.
    • fromIntegerList

      public static int[] fromIntegerList(List<Integer> src)
      Converts a list of Integer objects to a primitive array.
      Parameters:
      src - Source list to convert.
      Returns:
      An array containing the same values as the src list.
    • fromIntegerList

      public static int[] fromIntegerList(List<Integer> src, int[] dest)
      Converts a list of Integer objects to a primitive array.
      Parameters:
      src - Source list to convert.
      dest - Destination array to store values from src in (modified). Must be at least as large as src.
      Returns:
      A reference to the dest array.
    • fromList

      public static <T> T[] fromList(List<T> src, T[] dest)
      Converts a list to an array.
      Parameters:
      src - Source list to convert.
      dest - Destination array to store values from src in (modified). Must be at least as large as src.
      Returns:
      A reference to the dest array.
      Throws:
      IllegalArgumentException - If the dest array is not large enough to store all data of src list.
    • unbox

      public static double[] unbox(Double[] arr, double[] dest)
      Converts an array of Double objects to a primitive array (i.e. unboxing).
      Parameters:
      arr - Array to unbox.
      dest - Destination array for the unboxed values.
      Returns:
      If dest was not null then a reference to dest is returned. Otherwise, a new array with the unboxed values is returned.
    • unbox

      public static int[] unbox(Integer[] arr, int[] dest)
      Converts an array of Integer objects to a primitive array (i.e. unboxing).
      Parameters:
      arr - Array to unbox.
      dest - Destination array for the unboxed values.
      Returns:
      If dest was not null then a reference to dest is returned. Otherwise, a new array with the unboxed values is returned.
    • boxed

      public static Double[] boxed(double[] src)
      Converts a primitive array to an array of equivalent boxed type.
      Parameters:
      src - The source primitive array to box.
      Returns:
      A boxed array equivalent to the src primitive array.
    • boxed

      public static Integer[] boxed(int[] src)
      Converts a primitive array to an array of equivalent boxed type.
      Parameters:
      src - The source primitive array to box.
      Returns:
      A boxed array equivalent to the src primitive array.
    • asDouble

      public static double[] asDouble(int[] src, double[] dest)
      Converts an array of ints to an array of doubles.
      Parameters:
      src - Source array to convert.
      dest - Destination array to store double values equivalent to the values in the src array. If null, a new double array with the same size as src will be created.
      Returns:
      A reference to the dest array.
    • asDouble

      public static double[] asDouble(Integer[] src, double[] dest)
      Converts an array of Integers to an array of doubles.
      Parameters:
      src - Source array to convert.
      dest - Destination array to store double values equivalent to the values in the src array. If null, a new double array with the same size as src will be created.
      Returns:
      A reference to the dest array.