Hallo Oromo,

your solution only works on array of eqal lenght. If you decide to size 
array for example [4][2][3], your loops are going nowhere.
To make the code more flexible, you should study the following snippet:

int[][][] m = new int[4][2][3];

    int n = 1000;

    for(int x=0; x< m.length; x++) {
      for(int y=0; y < m[x].length; y++) {
           System.out.println("\n row & col " + x + " " + y);
        for(int z=0; z < m[x][y].length; z++) {
          m[x][y][z] = n++;
          System.out.print( m[x][y][z] + " " );

Remember multidimensional array are organized as arrays of arrays. In 
simple words, the first array contains the second array and the second
array contains the third array. These are only references. So you have 4 
references leading to 2 references leading to 3 references to store your 
data in memory.

Therefore the first loop test for the length of the first array (4 in my 
example).
In the second loop you get back the array length of each element in the 
first array (the size of 2).
The third loop is similar to the second. Consider that you have to use 
two indices to access each element of the second array. Each element has 
a length of 3
which is the size of the third array.

This construct is independent of the sizes of each array because it 
passes through each element of the 3-dimensional array no matter what 
geometry you use.
The code might seem a little complex but it is far less error-prone and 
flexible if you like to reuse your array in a class.

Best regards
Bodo Schulz

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to