## two Dimensioanl Array in Java

Java, supports multi-dimensional arrays - 1-dimensional, 2-dimensional, 3-dimensional. But we mostly use one and two dimensional arrays. Use of the 3 d and more dimensional array is rare. But once you understand the concept of the 2-dimensional array, you can extend the concept for 3 or more dimension case as well.

You can visualize a 2-dimensional array as a table with rows and columns. From machine perspective the row and colunms are just ideas as they do nor exist in reality. What happens on a machine is - we store the first row followed immediately by the second row and so on - in a linear fashion. We call a 2-dimensional array as an Array of Array.

The example below shows a two dimensional array, where a student records the number of hours he studied in each of the three weeks. We then calculate the total number of hours read by the student.

 ` /*  ReferenceDesigner.com Java Tutorial  understanding Two dimensional arrays  */  class twodarray{ public static void main (String args[]) {  int[][] hoursread = new int[2][7]; // Two rows 7 columns int i,j,totalhoursread =0;   // Assigning values to the first row  hoursread[0][0] = 2; hoursread[0][1] = 1; hoursread[0][2] = 3; hoursread[0][3] = 5; hoursread[0][4] = 4; hoursread[0][5] = 1; hoursread[0][6] = 0;   // Assigning values to the first row  hoursread[1][0] = 3; hoursread[1][1] = 1; hoursread[1][2] = 2; hoursread[1][3] = 6; hoursread[1][4] = 3; hoursread[1][5] = 2; hoursread[1][6] = 1;    for ( i=0; i<=1; i++) { for (j=0; j<=6; j++) totalhoursread += hoursread[i][j]; } System.out.println(" Total Hours Read = " + totalhoursread); }  }`

If you compile and run, you get the following output

 ` Total Hours Read = 34`

A 2x3 dimensional array x[2][3] can me mentally thought of as being composed of two rows an three columns.

Table: A two dimensional array x[2][3]

 x[0][0] x[0][1] x[0][2] x[1][0] x[1][1] x[1][2]