[英]Split 3 dimensional Array in multiple 2 dimensional Arrays in C#
在 Java 你可以這樣做:
int [][][] a = new int[2][10][10];
int [][] b = a[0];
但是在 c# 你不能這樣做:
int [,,] a = new int[2,10,10];
int [,] b = a[0];
我將如何在 c# 中執行此操作。 我只知道如果我想獲得一行,我可以這樣做:
int[,] a = new int[2,10];
int[] b = a.GetRow(0);
正如Poul Bak所建議的,您可以在 C# 中使用鋸齒狀的arrays ,如下所示:
int[][][] arr = new int[2][][];
for (int row = 0; row < arr.Length; row++)
{
arr[row] = new int[10][];
for (int col = 0; col < arr[row].Length; col++)
{
arr[row][col] = new int[10];
}
}
for (int row = 0; row < arr.Length; row++)
for (int col = 0; col < arr[row].Length; col++)
for (int plane = 0; plane < arr[row][col].Length; plane++)
arr[row][col][plane] = row*100 + col*10 + plane;
int[][] b = arr[1];
for (int col = 0; col < b.Length; col++)
{
Console.WriteLine();
for (int plane = 0; plane < b[col].Length; plane++)
Console.Write($" {b[col][plane],3}");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.