Thread: 3D array initializer list

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    65

    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. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    edited
    Last edited by C_ntua; 10-30-2009 at 08:38 AM.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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];
    					}
    				}
    			}
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    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. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by kenryuakuma View Post
    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 subscribe to a feed

Similar Threads

  1. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM