阅读量:2
Java中二维数组赋值的方式有以下几种:
- 逐个元素赋值:
int[][] array = new int[3][3]; array[0][0] = 1; array[0][1] = 2; array[0][2] = 3; // ...
- 使用循环赋值:
int[][] array = new int[3][3]; for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { array[i][j] = i + j; } }
- 使用静态初始化赋值:
int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
- 使用动态初始化赋值:
int[][] array = new int[3][]; array[0] = new int[]{1, 2, 3}; array[1] = new int[]{4, 5, 6}; array[2] = new int[]{7, 8, 9};