[英]How to convert Multidimensional int Array to a String List?
我想轉換我的array2D:
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
到列表:
List<String> numbers = new List<string>(array2d.ToString());
您可以使用Enumerable.Cast
展平多維數組
List<String> number = array2D.Cast<int>().Select(i => i.ToString()).ToList();
您可以將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.