java - 使用二维数组查找列中的最大数字

标签 java arrays

我试图编写一种方法来查找列中最大的数字。但是,我似乎无法找到一种方法来返回列中的最高数字,而不是考虑数组中组合的所有数字。我非常感谢任何评论或反馈!

这是我的代码:

public static void max1(int[][] score) {
    for (int i = 0; i < score.length; i++) {
        int max = score[0][0];
        for (int j = 0; j < score[i].length; j++)
            if (score[i][j] > max)
                max = score[i][j];
        System.out.println(max + "      ");
    }
}

最佳答案

您正在尝试查找行中而不是列中的最大值。对代码进行一些更改

public static void max1(int[][] score) {
    for (int i = 0; i < score[0].length; i++) { // i should be your column
        int max = score[0][i];// assign 1st value of the column as max
        for (int j = 0; j < score.length; j++){ // j is your row
            if (score[j][i] > max){ // check the column elements instead of row elements
                max = score[j][i];// get the column values 
            }
        }
        System.out.println(max + "      ");
    }
}

关于java - 使用二维数组查找列中的最大数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22064558/

相关文章:

java - 在 Android 中制作可滑动选项卡布局的最简单方法

java - 在 Java 中安全解码 Base64 字符数组

java - 当在 hashmap 中放入相同的键时,hashmap 是否会在与 equals 比较之前也比较引用?

java - 如何以编程方式处理超出免费 Firebase 数据库限制可能发生的错误

java - Spring Boot获取请求: Failed to convert type java. lang.String到所需类型int

php - 使用公共(public)列检索所有登录的 session 数据

python - 将不同维度的图像存储在 numpy 数组中

python - 计算数组中某个值的平均位置快速方法

java - SPOJ 生命宇宙和一切

C++ char数组复制到unsigned char数组