Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Paul Clapham
• Ron McLeod
• Jeanne Boyarsky
• Tim Cooke
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Henry Wong
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Stephan van Hulst
• Carey Brown
• Frits Walraven
Bartenders:
• Piet Souris
• Himai Minh

# Need explanation about three dimensional arrays

Ranch Hand
Posts: 169
• Number of slices to send:
Optional 'thank-you' note:
Hi,

I didnt understand three - D arrays concept.

Give me some explanation regardign three - D array concept.

Please explain abt this question's output:

1. public class Test {
2. public static void main(String [] args) {
Self Test 49
3. int [] [] [] x = new int [3] [] [];
4. int i,j;
5. x[0] = new int[4][];
6. x[1] = new int[2][];
7. x[2] = new int[5][];
8. for (i=0; i<x.length; i++)
9. for (j=0; j<x[i].length; j++) {
10. x[i][j] = new int [i + j + 1];
11. System.out.println("size = " + x[i][j].length);
12. }
13. }
14. }

Thanks.

Greenhorn
Posts: 21
• Number of slices to send:
Optional 'thank-you' note:
Consider this ,
int[][][] arr = new int[3][2][3];

this means:
[0]--------->[0]------------->[0][1][2]
[1]------>[0][1][2]

[1]---->[0]------------------>[0][1][2]
[1]------------>[0][1][2]

[2]-->[0]------------->[0][1][2]
[1]--------->[0][1][2]
Whenever I face mutidimentional array problem ..if it feels tough for me, I draw like this...thought it may help you.
Now the problem you have given..you can take it like this
at line 3,5,6,7
x=[0]----->[0][1][2][3]
[1]----->[0][1]
[2]----->[0][1][2][3][4]
Now after lines 8,9 and 10
x=[0]----->[0]---->[0] -1
[1]----->[0][1] -2
[2]------->[0][1][2]
[3]-------->[0][1][2][3]
[1]----->[0]---->[0]
[1]----->[0][1]
[2]----->[0]-------->[0][1][2]
[1]--------->[0][1][2][3]
[2]----------->[0][1][2][3][4]
[3]------------->[0][1][2][3][4][5]
[4]--------------->[0][1][2][3][4][5][6] -11
so this creates here 11 lines of output
for more clear view modify your line 11 as

System.out.println("x["+i+"]["+j+"] size = " + x[i][j].length);

Sita

Ja vardhan
Ranch Hand
Posts: 169
• Number of slices to send:
Optional 'thank-you' note:
Sita,

So sorrrrrrrrrry first of all, I didnt get what you mean here:

Consider this ,
int[][][] arr = new int[3][2][3];

this means:
[0]--------->[0]------------->[0][1][2]
[1]------>[0][1][2]

[1]---->[0]------------------>[0][1][2]
[1]------------>[0][1][2]

[2]-->[0]------------->[0][1][2]
[1]--------->[0][1][2]

What you mena to say with [0]--------->[0]------------->[0][1][2] ??

Seems you are from Andhra (yours is like Telugu name). is it ??

Thanks.

Greenhorn
Posts: 21
• Number of slices to send:
Optional 'thank-you' note:
Hi javardhan,

Let me try again,
int[][][] arr = new int[3][2][3];

this means:
arr[0]--------->a([0])------------->an1([0][1][2])
a([1])-------------->an2([0][1][2])

Here arr[0] refers to an Array with length "2".For your convinience I have marked it as "a".Similarly,the first element of array "a" rfers to another array of int with length 3 so comes the an1.

Now coming to a[1] it refers to another array of length 3.

Hope my trial is not ruin.

Sita

Greenhorn
Posts: 21
• Number of slices to send:
Optional 'thank-you' note:
If my previous posting doesnt clear the concept...I might be confusing you.

Sorry..

Sita

Ja vardhan
Ranch Hand
Posts: 169
• Number of slices to send:
Optional 'thank-you' note:
Sita,