First of all, forEach is a terminal statement with a return type of void , so you cannot use
stream().forEach(...).toArray();
You need to map your data into double[] strings, and then collect them into double[][] , which will lead us to the second problem, since toArray() (from the stream of objects) returns an array of Object[] , which can't be stored in double[][] link. You have to use
A[] toArray(IntFunction<A[]> generator)
which will return the correct data type, so you can use
toArray(size -> new double[size][])
or its shorter version
toArray(double[][]::new)
Here is what your code looks like:
double[][] doubleArray = Arrays.stream(intArray) .map( pArray -> Arrays .stream(pArray) .mapToDouble(d -> d.doubleValue()) .toArray() ).toArray(double[][]::new); //which is equivalent of // ).toArray(size -> new double[size][]); //---[Test]--- for (double[] row : doubleArray){ System.out.println(Arrays.toString(row)); }
Output:
[1.0, 2.0, 3.0] [4.0, 5.0, 6.0] [7.0, 8.0, 9.0]
source share