Two Dimensional Arrays (Array of Arrays)

Initialization and Accessing Two Dimensional Arrays (Array of Arrays)

The previous program ArrayTest3 is modified as ArrayTest4 to illustrate two-dimensional array initialization.

public class ArrayTest4
{
  public static void main(String args[])
  {
    double marks[][] = {
	                 { 41.6, 51.6, 61.6, 71.6 },
		         { 42.7, 52.7, 62.7, 72.7 },
		         { 43.8, 53.8, 63.8, 73.8 }
		       };

    System.out.println("Rows count: " + marks.length);
    System.out.println("1st row element count: " + marks[0].length);
    System.out.println("1st element: " + marks[0][0]);

    System.out.println("\nMatrix Form Printing");
    for(int i = 0; i < marks.length; i++)
    {
       for(int j = 0; j < marks[i].length; j++)
       {
	  System.out.print(marks[i][j] + "\t");
       }
       System.out.println();       
    }
  }
}  
Two Dimensional Arrays (Array of Arrays)
Output screen of ArrayTest4.java
       double marks[][] = {
	                     { 41.6, 51.6, 61.6, 71.6 },
		             { 42.7, 52.7, 62.7, 72.7 },
		             { 43.8, 53.8, 63.8, 73.8 }
	                  };

The above code clearly indicates that two dimensional array is an array of arrays. The marks array contains 3 rows, each row containing 4 elements.

The above code can be modified as follows.

double d1[] = { 41.6, 51.6, 61.6, 71.6 };
double d2[] = { 42.7, 52.7, 62.7, 72.7 };
double d3[] = { 43.8, 53.8, 63.8, 73.8 };

double marks[][] = { d1, d2, d3 };

Three one-dimensional arrays d1, d2 and d3 are placed with marks object.

5 thoughts on “Two Dimensional Arrays (Array of Arrays)”

Leave a Comment

Your email address will not be published.