开发者

Help In understanding Multi Dimentional Arrays

I have been googling a lot to get tutorials to understand from 3-D arrays going but it seems all the tutorials are in 2-D arrays. So i need a little explanation to understand from 3-D arrays going.

I really understand 2-Dimensional arrays very well but am finding it hard to understand 3 and 4 etc dimensional arrays. I know that 2-D arrays stands for rows and columns but what about this 3-D arrays:

My understanding of the code below is that there should be 3 rows and 4 columns but the 5 at the last position makes it look different. I need help in understanding why there are 12 rows and 5 columns. And also if you can help me in understanding 4 or 5-D arrays

Thanks.

开发者_开发百科 class ThreeDMatrix

{

public static void main(String[] args) 

{

int threeD[][][] = new int[3][4][5];

int i,j,k;

for (i=0; i<3; i++)

for(j=0; j<4; j++)

for (k=0; k<5; k++)

threeD[i][j][k]= i*j*k;


{

for(i=0; i<3; i++)

for(j=0; j<4; j++){

for (k=0; k<5; k++ )

{

System.out.print("-" + threeD[i][j][k]); 

}

System.out.println();

}

System.out.println();

}

}
}

OUTPUT:

-0-0-0-0-0

-0-0-0-0-0

-0-0-0-0-0

-0-0-0-0-0

-0-0-0-0-0

-0-1-2-3-4

-0-2-4-6-8

-0-3-6-9-12

-0-0-0-0-0

-0-2-4-6-8

-0-4-8-12-16

-0-6-12-18-24

Please sorry for the poor rendering of the code. I don't really know how to use the code tags properly. If someone will show me then i will repost . thanks


You can see your three dimensional array as 3 bidimensional array of 4x5 elements, or 12 one dimensional array of 5 elements.

That's why you have 12 rows in your output.

Think about this array as an int[12][5] where the first index depends on two different indices. This thing applies also to n-dimensional array. It's just a matter of indices..

If you really need help imagining it you should first consider a bidimensional array, in your example a 4x5 array.. so you have something like

00 01 02 03 04
10 11 12 13 14
20 21 22 23 24
30 31 32 33 34

when you add a third dimension is like having multiple matrices that stay one on each other..

so if you have int[2][4][5] you'll have

first index == 0

00 01 02 03 04
10 11 12 13 14
20 21 22 23 24
30 31 32 33 34

and another one

first index == 1

00 01 02 03 04
10 11 12 13 14
20 21 22 23 24
30 31 32 33 34

Maybe this picture can clarify:

Help In understanding Multi Dimentional Arrays


(source: microsoft.com)

Every sheet extracted from the "solid" represents a 2D array, infact as you can see every sheet has the same first index (0 for the first, 1 for the second, and so on). So by specifying the first index you select a matrix, then you can select the precise element by using the other two indices.

Of course you can extend this to any dimension, but it would be more difficult to show you a hyper-solid that represents the array.


int foo[][][] declares an array of arrays of arrays of int. Each new "dimension" just means "take whatever I have, and get me an array of those". Whether you call them "rows", "columns" or whatever is entirely up to you.

In your example, you see 12 lines because an effect of your nested loops is that you call 12 times System.out.println(). 12 newline characters mean 12 lines. This does not relate to the structure of the array, only to the way you print the data.


:-) There are three groups of sub-groups of four, each with 5 integer. Each line in your printout is a sub group of 5 integer. Four successive lines make one group. If you add a "-" at the second System.out.println your printout would be easier to read

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜