1

My problem is I want to initialize a multidimensional array declared like this:

int[][][] my3DArray;

However, the following code gives me an error on [sizeY][sizeZ] saying it expected ',' or ']'.

void Set3DArraySize(int sizeX, int sizeY, int sizeZ)
{
    my3DArray = new int[sizeX][sizeY][sizeZ];
}

When i declare the array like this though:

int[,,] my3DArray;

I am able to initialize it without any problems by doing it like this:

my3DArray = new int[sizeX,sizeY,sizeZ];

But then the problem becomes that if I try to get the length of one of the arrays I can't do for example my3DArray[x,y].Length and am instead forced to pass all of the indexes together my3DArray[x,y,z].

So, is there any way I can initialize the array with it being declared as int[][][] my3DArray;? Or will I have to store the sizes of the arrays elsewhere and use it like this int[,,] my3DArray;?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    ```int[][][]``` is a Jagged Array and not a multidimensional one. You can see more info on Multidimensional here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/multidimensional-arrays and Jagged Arrays here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays – Ivan Gechev Jul 14 '22 at 12:34

5 Answers5

1

You can do:

new int[][][] myArray = new int[][][] 
{
    new int[][] 
    {
        new int[] 
        {
            // Your numbers
        }
    }
}
myArray[0].Length
myArray[0][0].Length
myArray[0][0][0].Length

Works fine

MomoVR
  • 113
  • 1
  • 8
1

int[][] is a jaggad array, which means that every array can be in a different size. This is why you can initialize it that way:

    int[][] arr2D= new int[3][];
    arr2D[0] = new int[0];
    arr2D[1] = new int[1];
    arr2D[2] = new int[2];

which will create a 2d array that looks like this:

_
_ _
_ _ _

The following is an example to create 3d jaggad array (with different sizes for each dimension- 5 rows, 3 columns and 6 depth):

    int[][][] arr3D = new int[5][][];
    for (int col = 0; col < arr3D.Length; col++)
    {
        arr3D[col] = new int[3][];
        for (int depth = 0; depth < arr3D[col].Length; depth++)
        {
            arr3D[col][depth] = new int[6];
        }
    }

In order to get the dimension size in jaggad array, you can simply get the lenth of the array, but keep in mind that if the array is actually jagged, you will have different sizes for different arrays. if you initialize all of the arrays in a specific dimension with the same value, than you can check either of them, it will be the same.


int[,] is a multi dimensional array that every dimension have a fixed size. To create a multi dimensional: int[,] arr2D = new int[3,4];

which will create a 2d array looking like this:

_ _ _ _
_ _ _ _
_ _ _ _

In multi dimensional array you can't change the length of a specific row or column.

In most cases you will probably prefer a multi dimensional array.

In order to get the size of a specific dimension in a multi dimensional array, you can use the following method: int rows = arr22.GetLength(0); int cols = arr22.GetLength(1); int depth = arr22.GetLength(2);

The input is the dimension you want the size of.

Blizz
  • 23
  • 5
0

You can't declare a jagged array in one line like this: new int[sizeX][sizeY][sizeZ]; as you really have three levels of nested arrays. You need to initialize them at each level.

This is what that would look like:

void Set3DArraySize(int sizeX, int sizeY, int sizeZ)
{
    my3DArray = new int[sizeX][][];
    for (var x = 0; x < sizeX; x++)
    {
        my3DArray[x] = new int[sizeY][];
        for (var y = 0; y < sizeY; y++)
        {
            my3DArray[x][y] = new int[sizeZ];
        }
    }
}

Now, if you do want to initialize a multidimensional array, you can do this:

int[,,] z =
{
    { { 1, 2, }, { 1, 2, }, { 1, 2, }, },
    { { 1, 5, }, { 1, 5, }, { 2, 8, }, },
    { { 1, 5, }, { 1, 5, }, { 2, 8, }, },
    { { 1, 5, }, { 1, 5, }, { 2, 8, }, },
};

And then, to get the dimensions, you can do this:

Console.WriteLine(z.GetLength(0));
Console.WriteLine(z.GetLength(1));
Console.WriteLine(z.GetLength(2));

That gives me:

4
3
2

It's important to note that with a jagged array each nested array can be of different length, but with a multidimensional array then each dimension must be uniform.

For example, a jagged array could look like this:

int[][] y = new int[][]
{
    new [] { 1, 2, },
    new [] { 1, 2, 3, },
    new [] { 1, },
};

And a multidimensional array like this:

int[,] y =
{
    { 1, 2, 3, },
    { 4, 5, 3, },
    { 1, 6, 3, },
    { 1, 7, 8, },
};

I could not do this:

//illegal!!!
int[,] y = 
{
    { 1, 2, 3, },
    { 4, 5, 3, 7, },
    { 1, 6, },
    { 1, 7, 8, },
};
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • RE: Initializing jagged arrays, one might prefer this answer that already exists on SO, https://stackoverflow.com/a/1739058/659190 – Jodrell Jul 14 '22 at 13:36
-1

First,

int[][][]

is a jagged array, or an array of arrays of arrays. Where as,

int [,,]

is a multi-dimensioinal array. A single array with many dimensions.


While multi-dimensional arrays are easier to instantiate and will have fixed dimensions on every axis, historically, the CLR has been optimized for the vastly more common case of single dimensional arrays, as used in jagged arrays. For large sizes, you may experience unexpected performance issues.

You can still get the dimensions of a multi-dimensional array by using the GetLength method, and specifying the dimension you are interested in but, its a little clunky.

The number of dimensions can be retrieved from the Rank property.

You may prefer a jagged array and instantiating one axis at a time.

To instantiate jagged arrays, see this Initializing jagged arrays.

Jodrell
  • 34,946
  • 5
  • 87
  • 124
  • Thank you, the last link you provided was exactly what I was looking for. – Rafael Silva Jul 14 '22 at 13:12
  • "they have been overlooked and neglected and prone to unexpected performance issues" - what? Can you please elaborate on this? Any evidence? – Enigmativity Jul 14 '22 at 13:18
  • @Enigmativity https://stackoverflow.com/a/468873, to start with. – Jodrell Jul 14 '22 at 13:32
  • @Jodrell - That has nothing about being "overlooked" or "prone to unexpected performance issues". And I'd argue they haven't been "neglected". Those choices of words gives the impression that there are bugs or inefficient code in the compiler. It's more accurate to say that the common case has been heavily optimised. – Enigmativity Jul 14 '22 at 13:38
  • @Enigmativity, as reasonable point. – Jodrell Jul 14 '22 at 13:43
  • @Jodrell - "heavily de-optimized and may have unexpected performance issues"???? No-one has de-optimized anything and there are no unexpected performance issues. – Enigmativity Jul 14 '22 at 13:54
  • @Enigmativity, have you read the link? I've toned down my comment but I can personally vouch for historical performance issues with multi-dimensional arrays. I've no idea if those issues would persist with later versions of .Net but, I would consider it. – Jodrell Jul 14 '22 at 14:06
  • @Jodrell - Please understand I'm not saying that performance doesn't vary between the different types of arrays. I am saying that there are no "unexpected performance issues". The performance of all array types is clearly understood, so are not unexpected, and the use of the work "issue" implies a bug. You're unfairly categorising the differences, that's all. – Enigmativity Jul 14 '22 at 23:51
-1
void Set3DArraySize(int sizeX, int sizeY, int sizeZ)
{
   my3DArray = Enumerable.Repeat(Enumerable.Repeat(new int[sizeZ], sizeY).ToArray(),sizeX).ToArray();
}
Behnam
  • 337
  • 1
  • 6
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jul 15 '22 at 01:02