是否有一个单行代码(无循环)可以转换 List<double[]>
至 double[,]
?
最佳答案
转换为 double[,]
只能通过遍历列表来完成,并且要求列表中包含的所有数组大小相同:
double[,] arr = new double[list.Count, list[0].Length];
for (int i = 0; i < list.Count; i++)
{
for (int j = 0; j < list[0].Length; j++)
{
arr[i, j] = list[i][j];
}
}
当然,您可以通过调用 .ToArray()
轻松创建锯齿状的 double[][]
数组:
double[] array = new double[] { 1.0, 2.0, 3.0 };
double[] array1 = new double[] { 4.0, 5.0, 6.0 };
List<double[]> list = new List<double[]>();
list.Add(array);
list.Add(array1);
double[][] jaggedArray = list.ToArray();
关于c# - 从 List<double[]> 转换为 double[,],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3546020/