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

2019-07-28 04:52发布

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条回答
Deceive 欺骗
2楼-- · 2019-07-28 05:07

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));
}
查看更多
登录 后发表回答