[英]How to convert primitive double array to Double array
With the Apache common math library I get back a primitive double array. 使用Apache通用数学库,我可以获得原始的double数组。
RealMatrix pInverse = new LUDecomposition(p).getSolver().getInverse();
double[][] temp = pInverse.getData();
I need to convert temp to a Double[][]
我需要将temp转换为Double[][]
Double[][] inverse = new Double[][]temp;
If you are using Java 8+ you can use : 如果您使用的是Java 8+,则可以使用:
Double[][] inverse = Arrays.stream(temp)
.map(d -> Arrays.stream(d).boxed().toArray(Double[]::new))
.toArray(Double[][]::new);
As you are already using Apache Commons , it might be worth pointing out ArrayUtils.toObject 由于您已经在使用Apache Commons ,可能值得指出ArrayUtils.toObject
Converts an array of primitive doubles to objects. 将原始双精度数组转换为对象。
Using that, you could write Andreas first solution as 使用它,您可以将Andreas的第一个解决方案编写为
Double[][] inverse = new Double[temp.length][];
for (int i = 0; i < temp.length; i++) {
inverse[i] = ArrayUtils.toObject(temp[i]);
}
or YCF_L's solution as 或YCF_L的解决方案为
Double[][] inverse = Arrays.stream(temp)
.map(ArrayUtils::toObject)
.toArray(Double[][]::new);
It's a simple set of nested loop: 这是一组简单的嵌套循环:
Double[][] inverse = new Double[temp.length][];
for (int i = 0; i < temp.length; i++) {
inverse[i] = new Double[temp[i].length];
for (int j = 0; j < temp[i].length; j++)
inverse[i][j] = temp[i][j];
}
It's even shorter if you know all the sub-arrays are the same size: 如果您知道所有子数组的大小都相同,则它会更短:
Double[][] inverse = new Double[temp.length][temp[0].length];
for (int i = 0; i < temp.length; i++)
for (int j = 0; j < temp[0].length; j++)
inverse[i][j] = temp[i][j];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.