• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays doubt

 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, a multi-dimensional array is really just an array of arrays.

For example, start with a simple one-dimensional array...

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.
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marc,
Can you explain me with a example for 3 and 4 dimensional arrays?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marc and Krishnan explain the multi dimension arrays in nice way.

Where exactly do you face difficulty?


Thanks,
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANKS MARC AND KRISHNAN
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic