Home > database >  Split 3 dimensional Array in multiple 2 dimensional Arrays in C#
Split 3 dimensional Array in multiple 2 dimensional Arrays in C#

Time:09-11

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

CodePudding user response:

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