当前位置:首页 > Java 语言特性 > 正文

零基础学Java优学网二维数组课:从定义到实战,快速掌握数组操作技巧

// 就像这样定义一个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

零基础学Java优学网二维数组课:从定义到实战,快速掌握数组操作技巧

你可能想看:

相关文章:

文章已关闭评论!