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.

  1. /*
  2.   ReferenceDesigner.com Java Tutorial
  3.   understanding Two dimensional arrays
  4.   */
  5.  
  6. class twodarray{
  7. public static void main (String args[]) {
  8.  
  9. int[][] hoursread = new int[2][7]; // Two rows 7 columns
  10. int i,j,totalhoursread =0;
  11.  
  12. // Assigning values to the first row
  13.  
  14. hoursread[0][0] = 2;
  15. hoursread[0][1] = 1;
  16. hoursread[0][2] = 3;
  17. hoursread[0][3] = 5;
  18. hoursread[0][4] = 4;
  19. hoursread[0][5] = 1;
  20. hoursread[0][6] = 0;
  21.  
  22. // Assigning values to the first row
  23.  
  24. hoursread[1][0] = 3;
  25. hoursread[1][1] = 1;
  26. hoursread[1][2] = 2;
  27. hoursread[1][3] = 6;
  28. hoursread[1][4] = 3;
  29. hoursread[1][5] = 2;
  30. hoursread[1][6] = 1;
  31.  
  32.  
  33. for ( i=0; i<=1; i++)
  34. {
  35. for (j=0; j<=6; j++)
  36. totalhoursread += hoursread[i][j];
  37. }
  38. System.out.println(" Total Hours Read = " + totalhoursread);
  39. }
  40.  
  41. }


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]

Alternate way of Initializing Array


A two dimensional array could also be initialized as int [][]x ={ {3,2,1},{2,2,2}} Which is essentially a 2x3 array.

The following example initializes and prints the array in this fashion.

  1. /*
  2.   ReferenceDesigner.com Java Tutorial
  3.   understanding Two dimensional arrays
  4.   */
  5.  
  6. class twodarray{
  7. public static void main (String args[]) {
  8.  
  9. int [][]x ={ {7,2,4},{2,2,2}} ;
  10. int i,j;
  11.  
  12. for ( i=0; i<=1; i++)
  13. {
  14. for (j=0; j<=2; j++)
  15. System.out.print(" a["+i+"]["+j+"] = " + x[i][j]);
  16. System.out.println();
  17. }
  18.  
  19. }
  20. }


If you compile and run, you get the following output


a[0][0] = 3 a[0][1] = 2 a[0][2] = 1
 a[1][0] = 2 a[1][1] = 2 a[1][2] = 2




It is perfectly legat to have different sizes of the column. For example, it is legal to declare

int [][]x ={ {7,2,4},{2,2}, {2}} ;