Multi-dimensional arrays
final int[][] matrix = new int[2][3];
for (int row = 0; row < 2; row++) {
for (int col = 0; col < 3; col++) {
matrix[row][col] = col + row;
}
}
for (int row = 0; row < 2; row++) {
System.out.println(Arrays.toString(matrix[row]));
}
final int[][] matrix = new int[2][]; // Array containing two int arrays
matrix[0] = new int[3]; // first int array
matrix[1] = new int[3]; // second int array
No. 151
2-dimensional arrays and .length
Q: |
|
||||||
A: |
Two-dimensional arrays in Java™ are nested arrays of arrays. We thus choose appropriate data types:
|
final int[][] matrix = new int[][] {
{0, 1, 2},
{1, 2, 3}
};
|
[Jill, Tom] [Jane, Smith, Joe] [Jeff] |