List> to double[][] for JAMA or other libs

2019-07-28 04:41发布

问题:

i cannot get my List> to double[][] to work on linear algebra package like JAMA. Basically i have some sort of List with coordinates like this :

[[2.63, 11.087, -12.054], 
[2.357, 13.026, -15.29], 
[1.365, 16.691, -15.389], 
[0.262, 18.241, -18.694]]

And i am trying to put these coordinates to JAMA class which is double[][]. I tried to use method toArray, but i failed.

double[][] array = list.toArray(new double[list.size()][]);

How to do that? Or is there other packages to cope with SVD, which i need here, using List of Lists?

回答1:

Guava has a Doubles class which has a toArray that takes a Collection<? extends Number> and will thereby take a List<Double> and convert to an array. So...

List<List<Double>> myList;
double[][] myArray = new double[myList.size()][];
for (int i=0, n<myList.size(); i<n; i++){
     myArray[i] = Doubles.toArray(myList.get(i));
}