This example creates a table with two rows and three columns. Figure 2 presents a conceptual view of this table along with a memory view that shows how Java lays out this (and every) table in memory.

Figure 2 reveals that Java represents a two-dimensional array as a one-dimensional row array whose elements reference one-dimensional column arrays. The row index identifies the column array; the column index identifies the data item.

This syntax blends the previous two examples. Because the number of elements can be determined from the comma-separated lists of expressions, you don’t provide an `int_expr`

between either pair of square brackets. Here is an example:

```
new double [][] { { 20.5, 30.6, 28.3 }, { -38.7, -18.3, -16.2 } }
```

Two-dimensional arrays and array variables
By itself, a newly-created two-dimensional array is useless. Its reference must be assigned to an array variable of a compatible type, either directly or via a method call. The following syntaxes show how you would declare this variable:

```
```type var_name '[' ']' '[' ']'
type '[' ']' '[' ']' var_name

Each syntax declares an array variable that stores a reference to a two-dimensional array. It’s preferred to place the square brackets after `type`

. Consider the following examples:

```
double[][] temperatures1 = { { 20.5, 30.6, 28.3 }, { -38.7, -18.3, -16.2 } };
double[][] temperatures2 = new double[2][3];
double[][] temperatures3 = new double[][] { { 20.5, 30.6, 28.3 }, { -38.7, -18.3, -16.2 } };
```

Like one-dimensional array variables, a two-dimensional array variable is associated with a `.length`

property, which returns the length of the row array. For example, `temperatures1.length`

returns 2. Each row element is also an array variable with a `.length`

property, which returns the number of columns for the column array assigned to the row element. For example, `temperatures1[0].length`

returns 3.

Given an array variable, you can access any element in a two-dimensional array by specifying an expression that agrees with the following syntax:

```
```array_var '[' row_index ']' '[' col_index ']'

Both indexes are positive `int`

s that range from 0 to one less than the value returned from the respective `.length`

properties. Consider the next two examples:

```
double temp = temperatures1[0][1]; // Get value.
temperatures1[0][1] = 75.0; // Set value.
```

The first example returns the value in the second column of the first row (`30.6`

). The second example replaces this value with `75.0`

.

If you specify a negative index or an index that is greater than or equal to the value returned by the array variable’s `.length`

property, Java creates and throws an `ArrayIndexOutOfBoundsException`

object.

Multiplying two-dimensional arrays
Multiplying one matrix by another matrix is a common operation in fields ranging from computer graphics, to economics, to the transportation industry. Developers usually use the Matrix Multiplication algorithm for this operation.

How does matrix multiplication work? Let A represent a matrix with m rows and p columns. Similarly, let B represent a matrix with p rows and n columns. Multiply A by B to produce a matrix C, with m rows and n columns. Each cij entry in C is obtained by multiplying all entries in A’s ith row by corresponding entries in B’s jth column, then adding the results. Figure 3 illustrates these operations.

Jeff Friesen
Figure 3. Each of A’s rows is multiplied (and added) with each of B’s columns to produce an entry in C.

The following pseudocode expresses Matrix Multiplication in a 2-row-by-2-column and a 2-row-by-1-column table context. (Recall that I introduced pseudocode in Part 1 .)

```
// == == == == == ==
// | 10 30 | | 5 | | 10 x 5 + 30 x 7 (260) |
// | | X | | = | | // | 20 40 | | 7 | | 20 x 5 + 40 * 7 (380) | // == == == == == == DECLARE INTEGER a[][] = [ 10, 30 ] [ 20, 40 ]
DECLARE INTEGER b[][] = [ 5, 7 ]
DECLARE INTEGER m = 2 // Number of rows in left matrix (a)
DECLARE INTEGER p = 2 // Number of columns in left matrix (a) // Number of rows in right matrix (b)
DECLARE INTEGER n = 1 // Number of columns in right matrix (b)
DECLARE INTEGER c[m][n] // c holds 2 rows by 1 columns // All elements initialize to 0
FOR i = 0 TO m - 1 FOR j = 0 TO n - 1 FOR k = 0 TO p - 1 c[i][j] = c[i][j] + a[i][k] * b[k][j] NEXT k NEXT j
NEXT i
END
```

Because of the three `FOR`

loops, Matrix Multiplication has a time complexity of `O(`n ^{3} )

, which is pronounced “Big Oh of n cubed.” Matrix Multiplication offers cubic performance, which gets expensive time-wise when large matrixes are multiplied. It offers a space complexity of `O(`nm )

, which is pronounced “Big Oh of n *m ,” for storing an additional matrix of n rows by m columns. This becomes `O(`n ^{2} )

for square matrixes.

I’ve created a `MatMult`

Java application that lets you experiment with Matrix Multiplication. Listing 6 presents this application’s source code.

Listing 6. MatMult.java
```
public final class MatMult
{ public static void main(String[] args) { int[][] a = {{ 10, 30 }, { 20, 40 }}; int[][] b = {{ 5 }, { 7 }}; dump(a); System.out.println(); dump(b); System.out.println(); int[][] c = multiply(a, b); dump(c); } private static void dump(int[][] x) { if (x == null) { System.err.println("array is null"); return; } // Dump the matrix's element values to the standard output in a tabular // order. for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[0].length; j++) System.out.print(x[i][j] + " "); System.out.println(); } } private static int[][] multiply(int[][] a, int[][] b) { // ==================================================================== // 1. a.length contains a's row count // // 2. a[0].length (or any other a[x].length for a valid x) contains a's // column count // // 3. b.length contains b's row count // // 4. b[0].length (or any other b[x].length for a valid x) contains b's // column count // ==================================================================== // If a's column count != b's row count, bail out if (a[0].length != b.length) { System.err.println("a's column count != b's row count"); return null; } // Allocate result matrix with a size equal to a's row count times b's // column count int[][] result = new int[a.length][]; for (int i = 0; i < result.length; i++) result[i] = new int[b[0].length]; // Perform the multiplication and addition for (int i = 0; i < a.length; i++) for (int j = 0; j < b[0].length; j++) for (int k = 0; k < a[0].length; k++) // or k < b.length result[i][j] += a[i][k] * b[k][j]; // Return the result matrix return result; }
}
```

`MatMult`

declares a pair of matrixes and dumps their values to standard output. It then multiplies both matrixes and dumps the result matrix to standard output.

Compile Listing 6 as follows:

```
javac MatMult.java
```

Run the resulting application as follows:

```
java MatMult
```

You should observe the following output:

```
10 30 20 40 5 7 260 380
```

Example of matrix multiplication
Let's explore a problem that is best solved by matrix multiplication. In this scenario, a fruit grower in Florida loads a couple of semitrailers with 1,250 boxes of oranges, 400 boxes of peaches, and 250 boxes of grapefruit. Figure 4 shows a chart of the market price per box for each kind of fruit, in four different cities.

Jeff Friesen
Figure 4. Market price for oranges, peaches, and grapefruit in four different cities

Our problem is to determine where the fruit should be shipped and sold for maximum gross income. To solve that problem, we first reconstruct the chart from Figure 4 as a four-row by three-column price matrix. From this, we can construct a three-row by one-column quantity matrix, which appears below:

```
== ==
| 1250 |
| |
| 400 |
| |
| 250 |
== ==
```

With both matrixes on hand, we simply multiply the price matrix by the quantity matrix to produce a gross income matrix:

```
== == == ==
| 10.00 8.00 12.00 | == == | 18700.00 | New York
| | | 1250 | | |
| 11.00 8.50 11.55 | | | | 20037.50 | Los Angeles
| | X | 400 | = | |
| 8.75 6.90 10.00 | | | | 16197.50 | Miami | | | 250 | | |
| 10.50 8.25 11.75 | == == | 19362.50 | Chicago
== == == ==
```

Sending both semitrailers to Los Angeles will produce the highest gross income. But when distance and fuel costs are considered, perhaps New York is a better bet for yielding the highest income.

Ragged arrays
Having learned about two-dimensional arrays, you might now wonder whether it's possible to assign one-dimensional column arrays with different lengths to elements of a row array. The answer is yes. Consider these examples:

```
double[][] temperatures1 = { { 20.5, 30.6, 28.3 }, { -38.7, -18.3 } };
double[][] temperatures2 = new double[2][];
double[][] temperatures3 = new double[][] { { 20.5, 30.6, 28.3 }, { -38.7, -18.3 } };
```

The first and third examples create a two-dimensional array where the first row contains three columns and the second row contains two columns. The second example creates an array with two rows and an unspecified number of columns.

After creating `temperature2`

's row array, its elements must be populated with references to new column arrays. The following example demonstrates, assigning 3 columns to the first row and 2 columns to the second row:

```
temperatures2[0] = new double[3];
temperatures2[1] = new double[2];
```

The resulting two-dimensional array is known as a ragged array . Here is a second example:

```
int[][] x = new int[5][];
x[0] = new int[3];
x[1] = new int[2];
x[2] = new int[3];
x[3] = new int[5];
x[4] = new int[1];
```