Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
• Liutauras Vilda
• Tim Cooke
• Paul Clapham
• Jeanne Boyarsky
Sheriffs:
• Ron McLeod
• Frank Carver
• Junilu Lacar
Saloon Keepers:
• Stephan van Hulst
• Tim Moores
• Tim Holloway
• Al Hobbs
• Carey Brown
Bartenders:
• Piet Souris
• Frits Walraven
• fred rosenberger

# Arrays doubt

Ranch Hand
Posts: 652
• • Number of slices to send:
Optional 'thank-you' note:
• • Hi All,
Can anybody explain me 2,3,4 dimensional arrays with a example i am really confused?

Thanks all Ranch Hand
Posts: 178
• • Number of slices to send:
Optional 'thank-you' note:
• • 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; //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 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;//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 --> "col1"
k --> "col2"

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

Sheriff Posts: 11343   • • Number of slices to send:
Optional 'thank-you' note:
• • 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;

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

myArray is 0.
myArray is 0.
myArray is 0.

Now let's change it a little...

int[][] myArray = new int;

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 is an array with length 4.
myArray is an array with length 4.
myArray is an array with length 4.

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

myArray is an array with length 4, where...
myArray is 0.
myArray is 0.
myArray is 0.
myArray is 0.

myArray is an array with length 4, where...
myArray is 0.
myArray is 0.
myArray is 0.
myArray is 0.

myArray is an array with length 4, where...
myArray is 0.
myArray is 0.
myArray is 0.
myArray is 0.

Nik Arora
Ranch Hand
Posts: 652
• • Number of slices to send:
Optional 'thank-you' note:
• • Hi Marc,
Can you explain me with a example for 3 and 4 dimensional arrays? Ranch Hand
Posts: 1710
• • Number of slices to send:
Optional 'thank-you' note:
• • Marc and Krishnan explain the multi dimension arrays in nice way.

Where exactly do you face difficulty?

Thanks,

Nik Arora
Ranch Hand
Posts: 652
• • Number of slices to send:
Optional 'thank-you' note:
• • 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 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;//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   • • Number of slices to send:
Optional 'thank-you' note:
• • String[][][] j = new String;

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

Nik Arora
Ranch Hand
Posts: 652
• • Number of slices to send:
Optional 'thank-you' note:
• • THANKS MARC AND KRISHNAN  They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising