Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Head First Android this week in the Android forum!

# Multi-dim Array

Ranch Hand

Joined: Nov 05, 2007
Posts: 661
This is from Master exam

A.b2[1][1]=big;

B.b[1][0]=b3;

C.b2[0][1][1]=b;

D.b2[0][2][1]=b[1][0];

E.b2[1][1][0][1]=b[1][0];

F.b2[1][1]=b;

I get a little confused with multi dimentsional arrays, even thought i know the rules.
I know while assigning an array they should be of the same dimension
or you can assign a one dimensional to a 2 dimensional array
But it's difficult to implement it when you have arrays like

short b2[][][][]=new short[2][3][2][2];

I get totally lost here..

The only statment that makes sense to me is

b[1][0]=b3
Assigning a varibale to the 0 th element of the 1st array of b.

b2[1][1][0][1]=b[1][0];

Assigning an element of array b to an element of array b2.

The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3886

5

Think of it like a tree structure (that's how I think ).

when you say

[ September 17, 2008: Message edited by: Vijitha Kumara ]

SCJP 5 | SCWCD 5
sumi rankan
Ranch Hand

Joined: Apr 07, 2008
Posts: 46
I used a rough thumb rule for this type of question and it worked.
For eg,
short b2 [] [] [] []=new short [2][3][1][2];
while assigning, the dimensions on both sides should match.
Like when you say
b2[0][1]=b;
b is a 2d array and it has been assigned to the 3rd and 4th dim of b2;

b2[1][1][0]=b[0][0];//cannot assign
Here you can assign only a 1d array to b2.But an element of 2d array is being assigned.hence not possible.

b2[1][2][0]=b;//cannot assign.
Here a 1d array should be assigned not a 2d array.

b2[0][1][0][0]=b[0][0];//can assign.
here 2d array element is being assigned to 4d array element.

b2[0][1]=big;//can assign
same as b2[0][1]=b;

I understood it better.I am not so sure that I have given you an explanation in the way you can understand.
hope this helps!