简体   繁体   English

如何将多维int数组转换为字符串列表?

[英]How to convert Multidimensional int Array to a String List?

MultiDimensional 2D int array to string List 多维2D int数组到字符串列表


I would like to convert my array2D: 我想转换我的array2D:

int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

to a List: 到列表:

List<String> numbers = new List<string>(array2d.ToString());

您可以使用Enumerable.Cast展平多维数组

List<String> number = array2D.Cast<int>().Select(i => i.ToString()).ToList();

You can convert 2d array into jagged array and then convert it to List. 您可以将2d数组转换为锯齿状数组,然后将其转换为List。

int[,] arr = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };

int[][] jagged = new int[arr.GetLength(0)][];

for (int i = 0; i < arr.GetLength(0); i++)
{
jagged[i] = new int[arr.GetLength(1)];
for (int j = 0; j < arr.GetLength(1); j++)
{
    jagged[i][j] = arr[i, j];
}
}

List<int[]> list = jagged.ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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