How do you define and assign values to a two-dimensional array in Java?

The method of defining and assigning values to a two-dimensional array in Java is as follows:

// 定义一个二维数组
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;

Another shortcut is to assign values to a two-dimensional array at the time of its definition, as shown below:

int[][] array = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

This is how you can define and assign values to a two-dimensional array.

bannerAds