Hi,

For a simple view
Think single dimension array as pieces in a line. (width)
Think bidimensional array as pieces in lines and columns (surface). (width
and height)
Think tridimensional array as layers of bidimensional arrays (layers of
surfaces, volume) .

If you access an element which does not exists, you get
ArrayIndexOutOfBoundsException. You will now it then you encounter it. Java
does not force catching it because of few reasons(one of them is you are
knowing what are you doing).


Now, in Java dimensions must be created for the arrays. Considering that the
dimensions must be created with new, as it is written in Java tutorial here
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
A consequence of this is that the rows are allowed to vary in length for the
multidimensional arrays.

So, look at this examples for arrays. Don't be scared... try to understand
it. Anyway Java collections are coming soon...

   public static void main(String args[]) {

       //paralelipipedic
       int[][][] array3 = new int[3][2][3];//defined in place, after
declaration
       /*
        * 3 layers, 2 rows, 3 columns, total 3x2x3 elements
        *
        * [0][0][0],[0][0][1],[0][0][2]
        * [0][1][0],[0][1][1],[0][1][2]
        *
        * [1][0][0],[1][0][1],[1][0][2]
        * [1][1][0],[1][1][1],[1][1][2]
        *
        * [2][0][0],[2][0][1],[2][0][2]
        * [2][1][0],[2][1][1],[2][1][2]
        *
        */
       System.out.println("array3 has " + countArray3Elements(array3) + "
elements");

       //triangle
       int[][] array2w = new int[3][];
       for(int i = 0; i < 3; i++)
           array2w[i] = new int[i+1];
       /*
        * 3 rows with 1, 2, 3 elements, total 6 elements
        * [0][0]
        * [1][0],[1][1]
        * [2][0],[2][1],[2][2]
        *
        *
        * error
        * array2w[0][1] = 10;
        */
       System.out.println("array2w has " + countArray2Elements(array2w) + "
elements");


       //waves
       int[][] waves = new int[4][];
       waves[0] = new int[3];
       waves[1] = new int[2];
       waves[2] = new int[3];
       waves[3] = new int[2];

       /*
        * 4 rows, columns variable as follows: 3,2,3,2
        * elements: 3+2+3+2 = 10
        * [0][0],[0][1],[0][2]
        * [1][0],[1][1]
        * [2][0],[2][1],[2][2]
        * [3][0],[3][1]
        *
        * error
        * waves[1][1][2] = 10;//second row have only 2 elements
        */
       System.out.println("waves has " + countArray2Elements(waves) + "
elements");

       int[][][] pyramid = new int[3][][];
       //pyramid[0] = new int[1][1];
       //pyramid[1] = new int[2][2];
       //pyramid[2] = new int[3][3];

        //for passionate, you can do also this
        for(int i = 0; i < 3; i++)
           pyramid[i] = new int[i+1][i+1];


       /*
        * Think as layers in a pyramid, starting from top
        * tip is one element
        * middle is 4 elements
        * base is 9 elements
        *
        * [0][0][0]
        *
        * [1][0][0],[1][0][1]
        * [1][1][0],[1][1][1]
        *
        * [2][0][0],[2][0][1],[2][0][2]
        * [2][1][0],[2][1][1],[2][1][2]
        * [2][2][0],[2][2][1],[2][2][2]
        *
        * error
        * pyramid[0][0][1] = 10;//first layer has only one element,
[0][0][0]
        */
       System.out.println("pyramid has " + countArray3Elements(pyramid) + "
elements");

   }

   /* counts bidimensional array elements */
   private static int countArray2Elements(int[][] arr){
        int counter = 0;
        for(int i = 0; i < arr.length; i++)
            counter += arr[i].length;
        return counter;
   }

   /* counts tridimensional array elements */
   private static int countArray3Elements(int[][][] arr){
       int counter = 0;
       for(int i = 0; i < arr.length; i++)
           counter += countArray2Elements(arr[i]);
      return counter;
   }


Best regards,
Vasile Braileanu

--~--~---------~--~----~------------~-------~--~----~
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