This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Refactoring for Software Design Smells: Managing Technical Debt this week in the OO, Patterns, UML and Refactoring forum!

# Arrays doubt

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi All,
Can anybody explain me 2,3,4 dimensional arrays with a example i am really confused?

Thanks all
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
See if this helps.

View a multi-dimensional array as an array of arrays.

//1-d array , basically list of elements. view this as a single row with multiple columns.

String[] a = {"col1","col2"};

//2-d (array of arrays)-
String[][] i = new String[3][2]; //this will be 3 rows of 2 columns each (matrix)
//valid subscripts --> (0,0),(0,1),(1,0),(1,1),(2,0),(2,1)

i = new String[][]{a,a,a}; //I'm assigning 1-d array for each of the elements.

//3-d array of 2-D array
String[][][] j = new String[4][3][2];//4 arrays, each with 3 rows and 2 columns
j = new String[][][]{i,i,i,i};//assigning 2-d arrays for each of the elements

//4-d Array of 3-D array
String[][][][] k = new String[5][4][3][2];//is an array of 5 arrays each with 4 arrays, where each of those 4 arrays in turn have 3 arrays of 2 elements each.

k = new String[][][][]{ j,j,j,j,j};//assigning 3-d arrays for each elements

accessing an element:
k[2][0][0][0][0] --> "col1"
k[2][0][0][0][1] --> "col2"

and so on....
[ May 03, 2007: Message edited by: M Krishnan ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

In Java, a multi-dimensional array is really just an array of arrays.

int[] myArray = new int[3];

Here, myArray references an array with a length of 3. And each of the 3 elements are ints (initialized to zero). In particular...

myArray[0] is 0.
myArray[1] is 0.
myArray[2] is 0.

Now let's change it a little...

int[][] myArray = new int[3][4];

As before, myArray still references an array with a length of 3. But instead of having ints as elements, it now has arrays as elements.

myArray[0] is an array with length 4.
myArray[1] is an array with length 4.
myArray[2] is an array with length 4.

And each of these arrays contain ints (initialized to zero).

myArray[0] is an array with length 4, where...
myArray[0][0] is 0.
myArray[0][1] is 0.
myArray[0][2] is 0.
myArray[0][3] is 0.

myArray[1] is an array with length 4, where...
myArray[1][0] is 0.
myArray[1][1] is 0.
myArray[1][2] is 0.
myArray[1][3] is 0.

myArray[2] is an array with length 4, where...
myArray[2][0] is 0.
myArray[2][1] is 0.
myArray[2][2] is 0.
myArray[2][3] is 0.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Marc,
Can you explain me with a example for 3 and 4 dimensional arrays?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1710
Marc and Krishnan explain the multi dimension arrays in nice way.

Where exactly do you face difficulty?

Thanks,

cmbhatt
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Hi Krishnan,

Can you explain me the below statements in detail. I didnt get.

//3-d array of 2-D array
String[][][] j = new String[4][3][2];//4 arrays, each with 3 rows and 2 columns
j = new String[][][]{i,i,i,i};//assigning 2-d arrays for each of the elements

//4-d Array of 3-D array
String[][][][] k = new String[5][4][3][2];//is an array of 5 arrays each with 4 arrays, where each of those 4 arrays in turn have 3 arrays of 2 elements each.

k = new String[][][][]{ j,j,j,j,j};//assigning 3-d arrays for each elements
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

String[][][] j = new String[4][3][2];

j references a 3-dimensional array, with dimensions [4][3][2].
j[x] references a 2-dimensional array, with dimensions [3][2].
j[x][y] references a 1-dimensional array, with length 2.
j[x][y][z] references a String.
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
THANKS MARC AND KRISHNAN

Have you checked out Aspose?

subject: Arrays doubt