- 使用静态初始化的方式定义二维数组:
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
- 使用动态初始化的方式定义二维数组:
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;
- 使用动态初始化的方式定义不规则二维数组:
int[][] array = new int[3][];
array[0] = new int[2];
array[1] = new int[3];
array[2] = new int[4];
array[0][0] = 1;
array[0][1] = 2;
array[1][0] = 3;
array[1][1] = 4;
array[1][2] = 5;
array[2][0] = 6;
array[2][1] = 7;
array[2][2] = 8;
array[2][3] = 9;