java 二维数组 三维数组示例
时间: 2023-10-07 14:05:58 浏览: 103
Java中二维数组和三维数组的示例代码如下:
二维数组示例:
```java
int[][] matrix = new int[3][4];
// 初始化二维数组
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
matrix[i][j] = i + j;
}
}
// 输出二维数组
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " "); }
System.out.println();
}
```
三维数组示例:
```java
int[][][] cube = new int[3][4][2];
// 初始化三维数组
for (int i = 0; i < cube.length; i++) {
for (int j = 0; j < cube[i].length; j++) {
for (int k = 0; k < cube[i][j].length; k++) {
cube[i][j][k] = i + j + k;
}
}
}
// 输出三维数组
for (int i = 0; i < cube.length; i++) {
for (int j = 0; j < cube[i].length; j++) {
for (int k = 0; k < cube[i][j].length; k++) {
System.out.print(cube[i][j][k] + " ");
}
System.out.println();
}
System.out.println();
}
```
阅读全文