Class ArrayConversions
java.lang.Object
org.flag4j.util.ArrayConversions
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
Complex128andComplex64representations. - Boxing and unboxing of primitive and object types (e.g.,
int[]toInteger[]). - Converting arrays to
ArrayListand vice versa. - Transforming between primitive and custom numerical representations (e.g.,
int[]todouble[]).
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-
nullunless 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 Summary
Modifier and TypeMethodDescriptionstatic double[]asDouble(int[] src, double[] dest) Converts an array of ints to an array of doubles.static double[]Converts an array ofIntegersto an array of doubles.static Double[]boxed(double[] src) Converts a primitive array to an array of equivalent boxed type.static Integer[]boxed(int[] src) Converts a primitive array to an array of equivalent boxed type.static double[]fromDoubleList(List<Double> src) Converts a list ofDoublesobjects to a primitive array.static int[]fromIntegerList(List<Integer> src) Converts a list ofIntegerobjects to a primitive array.static int[]fromIntegerList(List<Integer> src, int[] dest) Converts a list ofIntegerobjects to a primitive array.static <T> T[]Converts a list to an array.toArrayList(double[] src) Converts an array of doubles to anarray list.toArrayList(int[] src) Converts an array of doubles to anarray list.static ArrayList<Complex128> toArrayList(Complex128[] src) Converts an array of complex numbers to anarray list.static Complex128[]toComplex128(double[] src, Complex128[] dest) Converts array to an array ofcomplex numbers.static Complex128[]toComplex128(int[] src, Complex128[] dest) Converts array to an array ofcomplex numbers.static Complex128[]toComplex128(Double[] src, Complex128[] dest) Converts array to an array ofcomplex numbers.static Complex128[]toComplex128(Integer[] src, Complex128[] dest) Converts array to an array ofcomplex numbers.static Complex128[]toComplex128(String[] src, Complex128[] dest) Converts array to an array ofcomplex numbers.static Complex128[]toComplex128(Complex64[] src, Complex128[] dest) Converts an array to an array ofcomplex numbers.static Complex64[]toComplex64(float[] src, Complex64[] dest) Converts array to an array ofcomplex numbers.static Complex64[]toComplex64(int[] src, Complex64[] dest) Converts array to an array ofcomplex numbers.static Complex64[]toComplex64(Float[] src, Complex64[] dest) Converts array to an array ofcomplex numbers.static Complex64[]toComplex64(Integer[] src, Complex64[] dest) Converts array to an array ofcomplex numbers.static Complex64[]toComplex64(String[] src, Complex64[] dest) Converts array to an array ofcomplex numbers.static ArrayList<Complex128> toComplexArrayList(double[] src) Converts an array of doubles to a complexarray list.static double[]Converts an array ofDoubleobjects to a primitive array (i.e. unboxing).static int[]Converts an array ofIntegerobjects to a primitive array (i.e. unboxing).
-
Method Details
-
toComplex128
Converts array to an array ofcomplex 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
destarray. - Throws:
IllegalArgumentException- If source and destination arrays do not have the same length.
-
toComplex128
Converts array to an array ofcomplex numbers.- Parameters:
src- Array to convert.dest- Destination array. If the destination array isnull, a new array will be created.- Returns:
- A reference to the
destarray. - Throws:
IllegalArgumentException- If source and destination arrays do not have the same length.
-
toComplex128
Converts array to an array ofcomplex 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
destarray. - Throws:
IllegalArgumentException- If source and destination arrays do not have the same length.
-
toComplex128
Converts array to an array ofcomplex 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
destarray. - Throws:
IllegalArgumentException- If source and destination arrays do not have the same length.
-
toComplex128
Converts an array to an array ofcomplex 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
destarray. - Throws:
IllegalArgumentException- If source and destination arrays do not have the same length.
-
toComplex128
Converts array to an array ofcomplex 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
destarray. - Throws:
IllegalArgumentException- If source and destination arrays do not have the same length.
-
toComplex64
Converts array to an array ofcomplex 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
destarray. - Throws:
IllegalArgumentException- If source and destination arrays do not have the same length.
-
toComplex64
Converts array to an array ofcomplex numbers.- Parameters:
src- Array to convert.dest- Destination array. If the destination array isnull, a new array will be created.- Returns:
- A reference to the
destarray. - Throws:
IllegalArgumentException- If source and destination arrays do not have the same length.
-
toComplex64
Converts array to an array ofcomplex 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
destarray. - Throws:
IllegalArgumentException- If source and destination arrays do not have the same length.
-
toComplex64
Converts array to an array ofcomplex 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
destarray. - Throws:
IllegalArgumentException- If source and destination arrays do not have the same length.
-
toComplex64
Converts array to an array ofcomplex 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
destarray. - Throws:
IllegalArgumentException- If source and destination arrays do not have the same length.
-
toArrayList
Converts an array of doubles to anarray list.- Parameters:
src- Array to convert.- Returns:
- An equivalent array list.
-
toArrayList
Converts an array of complex numbers to anarray list.- Parameters:
src- Array to convert.- Returns:
- An equivalent array list.
-
toComplexArrayList
Converts an array of doubles to a complexarray list.- Parameters:
src- Array to convert.- Returns:
- An equivalent complex array list.
-
toArrayList
Converts an array of doubles to anarray list.- Parameters:
src- Array to convert.- Returns:
- An equivalent array list.
-
fromDoubleList
-
fromIntegerList
-
fromIntegerList
Converts a list ofIntegerobjects to a primitive array.- Parameters:
src- Source list to convert.dest- Destination array to store values fromsrcin (modified). Must be at least as large assrc.- Returns:
- A reference to the
destarray.
-
fromList
Converts a list to an array.- Parameters:
src- Source list to convert.dest- Destination array to store values fromsrcin (modified). Must be at least as large assrc.- Returns:
- A reference to the
destarray. - Throws:
IllegalArgumentException- If thedestarray is not large enough to store all data ofsrclist.
-
unbox
Converts an array ofDoubleobjects to a primitive array (i.e. unboxing).- Parameters:
arr- Array to unbox.dest- Destination array for the unboxed values.- Returns:
- If dest was not
nullthen a reference todestis returned. Otherwise, a new array with the unboxed values is returned.
-
unbox
Converts an array ofIntegerobjects to a primitive array (i.e. unboxing).- Parameters:
arr- Array to unbox.dest- Destination array for the unboxed values.- Returns:
- If dest was not
nullthen a reference todestis returned. Otherwise, a new array with the unboxed values is returned.
-
boxed
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
srcprimitive array.
-
boxed
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
srcprimitive 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 thesrcarray. If null, a new double array with the same size assrcwill be created.- Returns:
- A reference to the
destarray.
-
asDouble
Converts an array ofIntegersto an array of doubles.- Parameters:
src- Source array to convert.dest- Destination array to store double values equivalent to the values in thesrcarray. If null, a new double array with the same size assrcwill be created.- Returns:
- A reference to the
destarray.
-