简体   繁体   中英

Convert List<int[]> to int[,]

How to convert List<int[]> to int[,] where all arrays in the list are int[2] ?

I'm trying:

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

You can't convert List to int[,] via LINQ .

You need to create new array [N, 2] and fill in a loop:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM