Multidimensional Array in Java

                                                     

An array that contains other arrays as its elements. The common type of multidimensional array is the two dimensional array. In Java, all multidimensional arrays are just arrays of arrays.

Think of the data stored in a table. The table has rows and columns, and at any specific coordinate(x,y), there is a piece of data. When working  with tabular data like this, you can refer to the location of each piece of data by its row number and column number.

 

 

For instance, in the following diagram, a set of letters is stored in a table. The numbers running down the left side of the table signify the row number, and the numbers running across the top of the table signify the column number. Oh, and just for consistency with our array discussions, I have chosed to begin the first row and column index with zero(0).

                                       COLUMNS

      (0)           (1)         (2)         (3)

R             (0)          A             B             C             D            

O             (1)          E              F              G            H

           (2)          I               J              K            L

             (3)          M            N             O            P

When you look at data  in table form like this, you can say things like "The letter B is at column 1, row 0" or "The letter A,B,C,D are in row 0, columns 0 through 3" . You can map any row and column index to a specific piece of data which is the whole beauty of tabular data.

                Now imagine that each row of data is a single char[]; in this case, you end up with four such arrays. Those four arrays could then be added to another array to give you the tabular form of the data. This final array would be a  two dimensional  array.

                You define a two-dimensional array by simply adding another set of brackets  to the declaration.  To declare the two dimensional array In this case, we must first determine the array type(data type). Because it will be holding the four simple char arrays as its elements, the two-dimensional array will be "an array of character arrays." Such an array would be declared as char[][] table.

                Okay, we have the array declaration. Now we need to populate it. If you keep in mind that each element of the table array is in fact a char array, this should not be as complex as you might think. All you have to do is create each  of the four simple arrays and add them one by one to the table array.

                The best way to see how this work is to jump right into the code, so let’s do it. The following class creates the two dimensional array. When you run the code , you specify a coordinate(rows and column in that order), and the letter found at that location prints. You can use the previous graphic as a point of reference to be sure that the code is working correctly.

class twodimm

{

     public static void main(String aa[])

       {

         if(aa.length<2)

          {

             System.out.println("enter the coordinates");

             System.exit(1);

         }

     char [] row0={'A','B','C','D'};

     char [] row1={'E','F','G','H'};

     char [] row2={'I','J','K','L'};

     char [] row3={'M','N','O','P'};

     

      char[][]table=new char[4][4];

      table[0]=row0;

      table[1]=row1;

      table[2]=row2;

       table[3]=row3;

      int row=Integer.parseInt(aa[0]);    

      int colum=Integer.parseInt(aa[1]);

      if(row>=table.length || colum>=table.length)

        {

           System.out.println("valid coordinates:"+row+","+colum);

           System.exit(1);

         }

      System.out.println("The data at"+row+","+colum+"is"+table[row][colum]);

     }

  }                                         

The twodimm class first ensures that you have entered two coordinates. These four(4) arrays represent each row & column in the table.  Just like the previous diagram, there are four rows and columns. All four

(          

            char [] row0={'A','B','C','D'};

            char [] row1={'E','F','G','H'};

            char [] row2={'I','J','K','L'};

             char [] row3={'M','N','O','P'};

)

of these char arrays are then added in order to the two-dimensional array named table.

                                            A quick check is made by the initial if statement to be sure that you eneterd valid coordinates to avoid any exceptions. It is easy to enter the wrong coordinates because you will be inclined to say that the character ‘P’ is located at row 4, column 4. Remember that the final row and column in this array is 3, not 4.

                                            When you execute this  program code and enter a pair of coordinates(2,2), it prints the letter found there. For example, if you start the program with java twodimm 2 2, the following message prints:

                                            The data is 2,2 is K

Output......

Multidimensional Array in Java