1

I need to convert a nested list of Double to a double[][]. I have tried using the code below, but the issue is how to convert to the primitive double. Any help would be much appreciated.

double[][] matrix = new double[listReturns.size()][];
    int i = 0;
    for (List<Double> nestedList : listReturns) {
        matrix[i++] = nestedList.toArray(new Double[nestedList.size()]);
    }

1 Answer 1

1

You can use streams:

double[][] mat =
    listReturns.stream() // Stream<List<Double>>
               .map(list -> list.stream() 
                                .mapToDouble(Double::doubleValue)
                                .toArray()) // map each inner List<Double> to a double[]
               .toArray(double[][]::new); // convert Stream<double[]> to a double[][]
Sign up to request clarification or add additional context in comments.

2 Comments

Just be warned that if this is for a college homework, and you give this answer, you better be prepared to know EVERYTHING about streams.
It solved the problem. This is not homework. Many thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.