【问题标题】:Convert List<int[]> to int[,]将 List<int[]> 转换为 int[,]
【发布时间】:2018-04-28 08:47:25
【问题描述】:

如何将List&lt;int[]&gt; 转换为int[,],其中列表中的所有数组都是int[2]

我正在尝试:

List<int[]> list = new List<int[]>();
list.Add(new int[2] { 3, 4 });
int[,] arr = list.ToArray();

【问题讨论】:

标签: c#


【解决方案1】:

您无法通过LINQ 将 List 转换为 int[,]。

你需要创建一个新数组[N, 2]并填充一个循环:

var arr = new int[list.Count, 2];

for (var i = 0; i < list.Count; i++)
{
    arr[i, 0] = list[i][0];
    arr[i, 1] = list[i][1];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    相关资源
    最近更新 更多