【问题标题】:Static methods and Jagged Arrays静态方法和交错数组
【发布时间】:2015-03-14 16:42:11
【问题描述】:

我正在尝试编写一个返回整数的静态方法,将整数的二维数组作为参数,并返回二维数组(锯齿状数组)中具有最大总和的行的索引它的所有元素。沿线出了点问题,我仍在试图弄清楚。请帮忙?
代码如下:

 public static int findMaxRow(int[][] maxRows){
        newI= 0;
        newJ= 0;
        for(int i=0; i< maxRows.length; i++) {
            newI += i;
            for(int j=0; j< maxRows.length; j++) {
                newJ += j;
           ``  if( newI > newJ){
               return newI;
             else {
             }
           }
         }
     }

【问题讨论】:

  • if 语句前有两个“`”,else 语句前缺少右括号“}”。此外,在您的代码中,newI 永远不会大于 newJ,因为 newI 只会添加 i maxRows.length 的值,而 newJ 将添加 j maxRows.length 的值^2 次。
  • @aokk3 检查我的答案

标签: java arrays jagged-arrays


【解决方案1】:

修改您的程序,以下示例将返回其中元素总和最大的行的索引。

假设我们要传递的数组是:

int [][] maxRows = {{1,2,3}, {1,2,3,4,5}, {9,9,9,9}, {1,2}};

在方法中传递这个数组

     public static int findMaxRow(int[][] maxRows){
        int sum = Integer.MIN_VALUE;

       int biggestIndex= 0;
       for(int i = 0; i<maxRows.length; i++){
           int temp = 0;
           for(int ir : maxRows[i]){
            temp+=ir;
           }
          if(temp>sum){
             sum = temp;
             biggestIndex = i;
         }
      }

            return biggestIndex;

     }

上述程序将返回元素总和最大的内部数组的索引,在上述情况下,它将返回2

【讨论】:

    【解决方案2】:

    您永远不会为newInewJ 定义类型,这可以通过在其声明前加上其预期类型(即int)来修复。在 if 语句之前还有两个“`”,并且在 else 语句之前缺少一个右括号“}”。但这些只是语法错误。修复这些错误后,您会注意到您的方法没有返回所需的结果。

    查看您的代码,特别是 for 循环。

    for(int i=0; i< maxRows.length; i++) {
        newI += i;
        for(int j=0; j< maxRows.length; j++) {
            newJ += j;
            // other stuff
        }
     }
    

    假设maxRows.length 等于3。这意味着外循环将从0 运行到2,因此newI 将等于3。同时对于外循环进行的每次迭代,内循环迭代3 次。所以newJ 最终将等于 9。这不是对数组元素求和的正确方法。一个更好的方法是遍历外循环中的数组并对内循环中的元素求和,然后进行比较以完成外循环。像这样:

    int largestRow = 0;
    int largestSum = 0;
    int sum;
    
    // iterate over each array
    for(int i=0; i< maxRows.length; i++) {
        sum = 0; // set and reset sum to zero
    
        // iterate over each element
        for(int j=0; j< maxRows[i].length; j++) {
            sum += maxRows[i][j];
        }
    
        // if sum is > the previous largest sum then set largest
        // sum to this new sum and record which row
        if(sum > largestSum) {
            largestRow = i;
            largestSum = sum;
        }
    }
    
    return largestRow;
    

    以下是您尝试完成的示例。

    public class RowSums {
    
        public static void main(String[] args) {
            int[][] test = { {1, 5, 7, 0, 9} , {2, 4, 5, 6, 7} , {9, 2, 0, 12, 8, 3} };
    
            System.out.println(printRows(test));
            System.out.println("The row with the largest sum is row "
                               + findMaxRow(test));
        }
    
        public static int findMaxRow(int[][] maxRows){
            int largestRow = 0;
            int largestSum = 0;
            int sum;
    
            // iterate over each array
            for(int i=0; i< maxRows.length; i++) {
                sum = 0; // set and reset sum to zero
    
                // iterate over each element
                for(int j=0; j< maxRows[i].length; j++) {
                    sum += maxRows[i][j];
                }
    
                // if sum is > the previous largest sum then set largest
                // sum to this new sum and record which row
                if(sum > largestSum) {
                    largestRow = i;
                    largestSum = sum;
                }
            }
    
            return largestRow;
         }
    
        public static String printRows(int[][] rows) {
            StringBuilder s = new StringBuilder("Rows and their sums:\n");
            int sum;
    
            for(int x = 0; x < rows.length; x++) {
                s.append("Row [" + x + "] = [ ");
                sum = 0;
                for(int y = 0; y < rows[x].length; y++) {
                    s.append(rows[x][y] + " ");
                    sum += rows[x][y];
                }
                s.append("]\n");
                s.append("Row [" + x + "]'s sum is " + sum + "\n");
            }
    
            return s.toString();
        }
    
    }
    

    输出:

    Rows and their sums:
    Row [0] = [ 1 5 7 0 9 ]
    Row [0]'s sum is 22
    Row [1] = [ 2 4 5 6 7 ]
    Row [1]'s sum is 24
    Row [2] = [ 9 2 0 12 8 3 ]
    Row [2]'s sum is 34
    
    The row with the largest sum is row 2
    

    【讨论】:

      猜你喜欢
      • 2013-10-20
      • 2013-12-31
      • 2012-12-04
      • 2013-11-29
      • 1970-01-01
      • 1970-01-01
      • 2019-06-29
      • 2020-12-27
      • 1970-01-01
      相关资源
      最近更新 更多