java - 如何将嵌套列表转换为二维数组
问题描述
我需要将嵌套的 Double 列表转换为 double[][]。我尝试使用下面的代码,但问题是如何转换为原始双精度。任何帮助将非常感激。
double[][] matrix = new double[listReturns.size()][];
int i = 0;
for (List<Double> nestedList : listReturns) {
matrix[i++] = nestedList.toArray(new Double[nestedList.size()]);
}
解决方案
您可以使用流:
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[][]
推荐阅读
- python - Python Pandas:将日期转换为文本,例如 2018 年 8 月 --> 08-2018 或 01-08-2018
- python - Python Regex - Unicode 文本匹配的位置和值
- java - 无法在 Servlet 应用程序中使用 Thymeleaf 访问请求属性或参数
- c - 整数指针的动态内存分配
- c# - 如何从 C# 中的多个进程附加到共享文件?
- java - 杰克逊从地图中获取列表,其中键包含路径(如“child_1_name”)
- mongodb - 带有ufw防火墙的ubuntu服务器上的MongoDB:连接超时
- javascript - false=="0" 结果为真,但 !!"0" 结果为真
- python-3.x - 为什么 OpenCV 不显示存储在 numpy 数组中的图像?(Python)
- google-cloud-platform - 如何以 root shell 重启实例?