Two Dimensional Arrays (Array of Arrays)

Two Dimensional Arrays (Array of Arrays)

Two Dimensional Arrays (Array of Arrays)

To inform the compiler that it is a two dimensional array, place two pairs of square braces either as prefix or suffix to array object. We can place any number of one dimensional arrays in a two dimensional array. A C/C++ programmer is much aware of "a two dimensional array is an array of one dimensional arrays", (Array of Arrays).

Creation, Assignment and Accessing two dimensional arrays

Following ArrayTest3 is an example of a two-dimensional array. A marks array object is created and elements are assigned later.

Two Dimensional Arrays (Array of Arrays)

Output screen of

double marks[][] = new double[3][4];

The above statement creates a two-dimensional array object marks that can store 12 elements of double data type, matrixed in 3 rows and 4 columns. Being double, the default value assigned for all the 12 elements is 0.0.

Following is the schematic representation of the task of the above program.

  1. marks.length gives the number of rows present in the two-dimensional array.
  2. marks[0].length gives the number of elements present in the first row. Ofcourse, it is the same for all the rows; but differs in the later program.
  3. There is no predefined way to find the total number of elements present in a two-dimensional array.
  4. marks[0][0] returns the element existing in the first row and first column.

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

Leave a Reply

Your email address will not be published. Required fields are marked *