二维数组初始化
时间: 2023-06-21 21:07:41 浏览: 110
二维数组可以使用以下方式进行初始化:
1. 静态初始化
```java
int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
```
2. 动态初始化
```java
int[][] array = new int[3][3];
array[0][0] = 1;
array[0][1] = 2;
array[0][2] = 3;
array[1][0] = 4;
array[1][1] = 5;
array[1][2] = 6;
array[2][0] = 7;
array[2][1] = 8;
array[2][2] = 9;
```
3. 指定行长度的动态初始化
```java
int[][] array = new int[3][];
array[0] = new int[3];
array[1] = new int[3];
array[2] = new int[3];
array[0][0] = 1;
array[0][1] = 2;
array[0][2] = 3;
array[1][0] = 4;
array[1][1] = 5;
array[1][2] = 6;
array[2][0] = 7;
array[2][1] = 8;
array[2][2] = 9;
```
4. 不规则数组的动态初始化
```java
int[][] array = new int[3][];
array[0] = new int[3];
array[1] = new int[2];
array[2] = new int[1];
array[0][0] = 1;
array[0][1] = 2;
array[0][2] = 3;
array[1][0] = 4;
array[1][1] = 5;
array[2][0] = 6;
```
阅读全文