0

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

1 Answer 1

1

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}");
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.