java怎么输出二维数组
时间: 2024-06-14 10:04:18 浏览: 132
以下是两种Java输出二维数组的方法:
1. 先声明,再创建二维数组对象
```java
public class Test {
public static void main(String[] args) {
int arr[][]; // 声明二维数组
arr = new int[3][4]; // 创建二维数组对象
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(arr[i][j] + " "); // 输出二维数组元素
}
System.out.println();
}
}
}
```
2. 在创建数组时直接指定数据内容
```java
public class Test {
public static void main(String[] args) {
int[][] arr = {{1, 2, 3}, {4, 5, 6}}; // 创建并初始化二维数组
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(arr[i][j] + " "); // 输出二维数组元素
}
System.out.println();
}
}
}
```
阅读全文
相关推荐

















