1

In java I could do it like this:

Square[][][] sheets;
public final Square[][] getSheet(int index) {
    return sheets[index];
}

What is the equivalent in c#? Since the syntax for multidimensional arrays is a little different:

Square[,,] Sheets;
public Square[,] GetSheet(int index) {
    return ????
}
5
  • 4
    Technically, that isn't a multidimensional array in java, that's an array of arrays. You can do the same in C#. Commented Nov 10, 2015 at 18:11
  • 1
    int[,,] array3D = new int[x, y, z]; is a 3d array in c# Commented Nov 10, 2015 at 18:13
  • It does not make sense to take 2 dimensions of 3d array(not talking about jagged array, they are 1d). You have to specify which dimensions? XY? XZ? YZ? Commented Nov 10, 2015 at 18:19
  • sheet index and x,y, so I suppose the best way to go about it would be: [][,] Commented Nov 10, 2015 at 18:21
  • @VincasStonys I agree - if you are really modelling an array of 2-D arrays then Square[][,] may be the cleanest structure. Commented Nov 10, 2015 at 18:32

1 Answer 1

6

The equivalent in C# is the same if you are using jagged arrays:

private Square[][][] sheets;
public Square[][] GetSheet(int index) 
{
    return sheets[index];
}

If you have a multi-dimensional array there's not a simple way to get two of the three dimensions. The best you can do is recreate an array:

private Square[,,] sheets;
public Square[,] GetSheet(int index) 
{
    int x = sheets.GetLength(0);      
    int y = sheets.GetLength(1); 

    Square[,] sheet = new Square[x,y];
    for(int i = 0; i < x; i++)
    for(int j = 0; j < y; j++)
         sheet[i,j] = sheets[i,z,index];

    return sheet;
}

How do I initialize a Square[4][4][4]?

You can either loop and initialize each dimension:

for(int i = 0; i < 4; i++)
{
    sheets[i] = new Square[4][];
    for(int j = 0; j < 4; j++)
    {
        sheets[i][j] = new Square[4];
    }
}

(Note that if Square is a reference type, then you also need to initialize each Square[i][j][k] to a value, otherwise it will be null)

or use array initialization syntax;

sheets = new Square[4][][]
{
     new Square[4][] {new Square[4], new Square[4], new Square[4], new Square[4],},
     new Square[4][] {new Square[4], new Square[4], new Square[4], new Square[4],},
     new Square[4][] {new Square[4], new Square[4], new Square[4], new Square[4],},
     new Square[4][] {new Square[4], new Square[4], new Square[4], new Square[4],},
};

Obviously, looping scales much more easily that initialization syntax.

Sign up to request clarification or add additional context in comments.

3 Comments

...or Square[][,] sheets if OP does not need to further project his squares.
I'm not tied to multi-dimensional arrays, so using jagged are fine. What is the correct way then to rewrite this from java to c#: Sheets = new Square[4][4][4]; ? Is it: Sheets = new Square[4][][]; ?
@VincasStonys That's the first step. you then need to loop and initialize each dimension (sheets[i] = new int[4][]; sheets[i][j] = new int[4];)

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.