A one-dimenional array is an array with one index:
A two-dimenional array is an array with two indices:
A 3x3 matrix: The unity matrix: +- -+ +- -+ | 2 5 8 | | 1 0 0 | | 3 0 3 | | 0 1 0 | | 5 9 1 | | 0 0 1 | +- -+ +- -+ |
myDataType[][] nameOfTheArrayVariable; |
The result of this definition is a variable that can contain a reference (address) to an array:
Notice that the array itself is NOT CREATED by this definition !
new myDataType[m][n]; |
The result of this new is (you have seen the new operation before and the effect has NOT changed !):
myDataType[][] nameOfTheArrayVariable; // Variable is defined but // is uninitialized ... (later:) nameOfTheArrayVariable = new myDataType[m][n]; // Now the variable is // being initialzied OR: myDataType[][] nameOfTheArrayVariable = new myDataType[m][n]; // Variable is defined and // initialized at the same time |
Examples:
Define a 3x4 matrix of double: double[][] a = new double[3][4]; Define a 4x5 matrix of int: int[][] b = new int[4][5]; |
nameOfTheArrayVariable[i][j] // element in the i^{th} row // and the j^{th} column |
Examples:
Assign the value 3.14 to a[2][3]: a[2][3] = 3.14; Assign the value 123 to b[2][1]: b[2][1] = 123; |