# Examples of Two Dimensional Arrays

• Introduction

• We will just look at one type two-dimensioanl arrays

• Doubles (primitive type)

• You can easily extend the concept to two-dimensioanl arrays of other types (as seen in this webpage: click here

• Working with a two-dimensional array of double variables

• Defining an array of double variables of 3 rows by 4 columns:

 ``` double[][] data = new double[3][4]; ```

The result of this definition is:

• Use data[i][j] to access the element in the ith row and jth column in the array

Example:

 ``` data[1][2] = 29.95; // Assign 29.95 to element // with row index 1 and column // index 2 in array data ```

The result of this assignment statement is:

NOTE: remember that computers start counting at 0 !!!

• Size of the array

• The variable data.length in an array object contains the length of the first dimension (= number of rows) in the array data

• The variable data[i].length in an array object contains the length of the ith row (= number of columns) in the array data

• Example:

 ``` double[] data = new double[10]; int i, j; // Give each element some value for (i = 0; i < data.length; i++) for (j = 0; j < data[i].length; j++) data[i][j] = i + j; // Print each element some value for (i = 0; i < data.length; i++) { // Print row i for (j = 0; j < data[i].length; j++) System.out.print("data[" + i + "][" + j + "] = " + data[i][j]); System.out.println(); } ```

• Example Program: (Demo above code)