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

一个人想着一个人 提交于 2019-12-24 13:59:00

问题


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));
}


来源:https://stackoverflow.com/questions/16689581/listlistdouble-to-double-for-jama-or-other-libs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!