【问题标题】:Java - Find max element of every row and max element of every column on 2D ArrayJava - 在二维数组上查找每行的最大元素和每列的最大元素
【发布时间】:2018-08-24 15:09:15
【问题描述】:

我想通过逐行检查来找到行的最大元素,对于列也是如此!

示例二维数组:

3 7 2 5 1 4 6 9 8

通过逐个元素检查每一行元素,它应该是这样的:第一行 7 是最大值,然后第二行没有大于 7 的元素,所以最大值仍然是 7,然后第三行 9 大于7 所以行的最大元素是 9!

列也是一样:第一列最大为 6,第二列最大为 9,第三列没有元素大于 9,因此最大列数为 9!

理论上行的最大元素也是列的最大元素!

所以,我需要编写一个 Java 程序来执行此操作,以便最大行数和最大列数的结果相等,这意味着我的程序可以正常运行!

在我的代码中,我已将二维数组存储在 bigMatrix[][] 上,但我不明白如何使用 bigMatrix[i] 获取整行,它有效,但我不明白如何,我不明白'不知道如何做同样的事情来获取每一列并将它们作为数组传递给我的函数getMax() 以找到该列的最大值!

**下面的代码可以很好地找到Rows的最大元素,但我不知道如何找到Columns的最大元素!

int rowMax = Integer.MIN_VALUE;

for(int i = 0; i < 3; i++) {
    if(rowMax < getMax(bigMatrix[i])) {
        rowMax = getMax(bigMatrix[i]);
    }
}

public static int getMax(int[] ourArray) {
    int max = Integer.MIN_VALUE;
    for (int i = 0; i < ourArray.length; i++) {
        max = Math.max(max, ourArray[i]);
    }
    return max;
}

【问题讨论】:

标签: java arrays multidimensional-array


【解决方案1】:

只需将getMax(int[] ourArray)函数的参数类型更改为getMax(int[][] ourArray),然后在调用函数时将二维数组作为参数传递即可。

在正文中,您需要遍历数组的每个项目,因此您需要 double 循环

for(int i = 0; i < ourArray.length;i++){
 for(int j = 0; j < ourArray[i].length;j++){
  max = Math.max(max, ourArray[i][j];
 }
}

【讨论】:

    【解决方案2】:

    为此创建一个循环,将每一列解析为 findMaxMethod 喜欢这个:

    public int method(int column)
    {
     int result = Integer.MIN_VALUE;
     for(int x = 0; x<array[column].length; x++)
       {
        result = Math.max(result, array[column][x]);
       }
     return result;
    }
    

    希望这会有所帮助....

    【讨论】:

      【解决方案3】:

      这里有一个解决你的问题的方法,假设数组中每一行的列数相同(注意以下情况需要考虑:(1)当数组为空时,(2)当数组为空时不为空且其中没有任何项目;这两种情况都在下面得到了适当的处理):

      public static void findAndPrintMaximumValues(int[][] arrayToTraverse) {
              if(arrayToTraverse == null || arrayToTraverse.length == 0 || arrayToTraverse[0].length == 0) {  
                  throw new IllegalArgumentException("Either the array is null or it doesn't contain any elements.");
              }
      
              int maximumValueGoingRowByRow = Integer.MIN_VALUE;
              int maximumValueGoingColumnByColumn = Integer.MIN_VALUE;
      
              // Traverse the array in row-major order.
              for(int currentRowIndex = 0; currentRowIndex < arrayToTraverse.length; currentRowIndex++) {
                  for(int currentColumnIndex = 0; currentColumnIndex < arrayToTraverse[0].length; currentColumnIndex++) {
                      maximumValueGoingRowByRow = Math.max(maximumValueGoingRowByRow, arrayToTraverse[currentRowIndex][currentColumnIndex]);
                  }
              }
      
              // Traverse the array in column-major order.
              for(int currentColumnIndex = 0; currentColumnIndex < arrayToTraverse[0].length; currentColumnIndex++) {
                  for(int currentRowIndex = 0; currentRowIndex < arrayToTraverse.length; currentRowIndex++) {
                      maximumValueGoingColumnByColumn = Math.max(maximumValueGoingColumnByColumn, arrayToTraverse[currentRowIndex][currentColumnIndex]);
                  }
              }
      
              System.out.format("The maximum value with traversal in row-major order is %d.%n", maximumValueGoingRowByRow);
              System.out.format("The maximum value with traversal in column-major order is %d.", maximumValueGoingColumnByColumn);
          }
      

      【讨论】:

        猜你喜欢
        • 2018-05-24
        • 1970-01-01
        • 1970-01-01
        • 2022-12-31
        • 1970-01-01
        • 1970-01-01
        • 2013-12-15
        • 2015-12-02
        • 2019-08-28
        相关资源
        最近更新 更多