简体   繁体   中英

Split 3 dimensional Array in multiple 2 dimensional Arrays in C#

In Java you can do:

int [][][] a = new int[2][10][10];
int [][] b = a[0];

But in c# you can't do:

int [,,] a = new int[2,10,10];
int [,] b = a[0];

How would I do this in c#. I just know that I can do this if I want to get a row:

int[,] a = new int[2,10];
int[] b = a.GetRow(0); 

As suggested by Poul Bak , you can use jagged arrays in C# as follows:

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}");
}

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