C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-30-2009, 08:26 AM   #1
Registered User
 
Join Date: Sep 2008
Posts: 62
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.
kenryuakuma is offline   Reply With Quote
Old 10-30-2009, 08:36 AM   #2
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,134
edited

Last edited by C_ntua; 10-30-2009 at 08:38 AM.
C_ntua is offline   Reply With Quote
Old 10-30-2009, 08:38 AM   #3
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,134
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
C_ntua is offline   Reply With Quote
Old 10-30-2009, 08:39 AM   #4
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,122
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.
Magos is offline   Reply With Quote
Old 11-01-2009, 08:46 PM   #5
Registered User
 
Join Date: Sep 2008
Posts: 62
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 }}}
kenryuakuma is offline   Reply With Quote
Old 11-15-2009, 01:40 AM   #6
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,134
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.
C_ntua is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:55 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22