java给二维数组赋值
时间: 2023-10-17 16:30:59 浏览: 76
java 二维数组
Java给二维数组赋值的方式有以下几种:
1. 直接赋值
```java
int[][] arr = {{1,2,3},{4,5,6},{7,8,9}};
```
2. 循环赋值
```java
int[][] arr = new int[3][3];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = i * 3 + j + 1;
}
}
```
3. 分步赋值
```java
int[][] arr = new int[3][3];
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1][0] = 4;
arr[1][1] = 5;
arr[1][2] = 6;
arr[2][0] = 7;
arr[2][1] = 8;
arr[2][2] = 9;
```
阅读全文