在Java中,可以通過以下方式定義和賦值二維數(shù)組:
// 定義一個二維數(shù)組,包含3行和4列
int[][] twoDArray = new int[3][4];
// 給二維數(shù)組賦值
twoDArray[0][0] = 1;
twoDArray[0][1] = 2;
twoDArray[0][2] = 3;
twoDArray[0][3] = 4;
twoDArray[1][0] = 5;
twoDArray[1][1] = 6;
twoDArray[1][2] = 7;
twoDArray[1][3] = 8;
twoDArray[2][0] = 9;
twoDArray[2][1] = 10;
twoDArray[2][2] = 11;
twoDArray[2][3] = 12;
也可以在定義二維數(shù)組的同時進行賦值:
int[][] twoDArray = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};