# Thread: 3D array initializer list

1. ## 3D array initializer list

Code:
`int [,] 2D = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};`
for two dimensional array, when we look at the initializer list, we can clearly know that what kind of 2 dimensional array it is. According to how the 2D array is outlined, we say it is 4 x 3 2D array. And then to access the value of 1, we would say use indices of 0, 0 and acess value 6, we use 1, 2.

But when it comes to 3D array, I am so confused.
Take this one for example, which I stole it from MSDN.

Code:
`int[, ,] array3D = new int[,,] { { { 1, 2, 3 } }, { { 4, 5, 6 } }}`
How can we tell what kind of 3D array it is? Is it 1 x 2 x 3 or what? And besides, what are the three indices to use to access the second or third value within the first set or second set of the curly braces? I am stuck and I hope there would be some help.

2. edited

3. It is a bad example, but it is a 2x1x3 array.
If it was a 1x2x3 it would be
Code:
`{{ { 1, 2, 3 } ,  { 4, 5, 6 } }}`
Just start from the inside out. Like
Code:
`{ { { 1, 2, 3 } }, { { 4, 5, 6 } }}`
2xsomething
Then you can tell that each something is a 1x3

4. I'd format it like this, for easier reading:
Code:
```			var x = new int[,,]
{
//A
{
//A.1
{
1,2,3,4,
},
//A.2
{
5,6,7,8,
},
//A.3
{
9,0,1,2,
},
},
//B
{
//B.1
{
3,4,5,6,
},
//B.2
{
7,8,9,0,
},
//B.3
{
1,2,3,4,
},
},
};

//Outer (A, B)
for(var i1 = 0; i1 < 2; i1++)
{
//Middle (1, 2, 3)
for(var i2 = 0; i2 < 3; i2++)
{
//Inner
for(var i3 = 0; i3 < 4; i3++)
{
var v = x[i1,i2,i3];
}
}
}```

5. Thanks so much for helping. But one more question.

I assume this is not possible, but I really wanna know if that really works.

Is it possible to initialize and declare a 3d array like this:

int[, ,] array3D = new int[,,] {{{ 1, 2, 3 }}, {{ 4, 5, 6 }, { 8,0,7 }}}

6. Originally Posted by kenryuakuma
Thanks so much for helping. But one more question.

I assume this is not possible, but I really wanna know if that really works.

Is it possible to initialize and declare a 3d array like this:

int[, ,] array3D = new int[,,] {{{ 1, 2, 3 }}, {{ 4, 5, 6 }, { 8,0,7 }}}
Try it. It shouldn't though. It doesn't have X*Y*Z elements. It is a 2*(1,2)*3.

Popular pages Recent additions