// 就像这样定义一个3行4列的二维数组 int[][] classroomSeats = new int[3][4];
int[][] scores;
int[][] scores = {
{85, 90, 78},
{92, 88, 95},
{76, 85, 90}
};
// 访问第二行第三列的元素 int targetScore = scores[1][2]; // 结果是95
int[][] matrixA = {{1, 2}, {3, 4}}; int[][] matrixB = {{5, 6}, {7, 8}}; int[][] result = new int[2][2];
for (int i = 0; i < matrixA.length; i++) {
for (int j = 0; j < matrixA[i].length; j++) {
result[i][j] = matrixA[i][j] + matrixB[i][j];
}
}
int[][] scores = new int[3][4]; // 错误:有效索引是0-2和0-3 scores[3][4] = 95; // ArrayIndexOutOfBoundsException
你可能想看: