【问题标题】:Finding the sum of the rows of a 2d array and returning it in an array查找二维数组的行的总和并将其返回到数组中
【发布时间】:2017-03-21 05:31:21
【问题描述】:

我正在尝试完成一个 AP CS FRQ 问题。我写了代码,但它不起作用。我哪里搞砸了?

编写一个静态方法rowSums,计算给定二维中每一行的总和 数组并在一维数组中返回这些和。该方法有一个参数,一个二维 int 值的数组 arr2D。该数组按行优先顺序排列: arr2D[r][c] 是条目 在 r 行和 c 列。该方法返回一个一维数组,其中每一行都有一个条目 arr2D 使得每个条目是 arr2D 中相应行的总和。提醒一下,每一行 二维数组是一维数组。

`   public static int[] rowSums(int[][] arr2D){
        int total2 = 0;
        int a[] = new int[arr2D.length];
        for(int x=0; x<arr2D.length; x++){
            for(int n=0; n<arr2D[x].length;n++){
                arr2D[x][n] = total2;
                a[x] = a[x] + total2;
            }
        }
        return a;
    }`

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    需要在外循环内重置total2,在内循环结束后设置值

        int a[] = new int[arr2D.length];
        for(int x=0; x<arr2D.length; x++){
            int total2 = 0;
            for(int n=0; n<arr2D[x].length;n++){
                  total2 += arr2D [x][n];
            }
            a[x] = total2;
        }
    

    如果total2 不会被重复使用,则可以缩短为

    for (int x=0; x < arr2D.length; x++) {
        for (int n=0; n<arr2D[x].length; n++) {
            a[x] = a[x] + arr2D[x][n];
        }
    }
    

    【讨论】:

      【解决方案2】:

      您的分配是向后的,您应该使用以下方法存储 2D 数组的每个元素:

      total2 = arr2D[x][n];
      

      不是这个:

      arr2D[x][n] = total2;
      

      完整代码:

      for (int x=0; x < arr2D.length; x++) {
          for (int n=0; n < arr2D[x].length; n++) {
              total2 = arr2D[x][n];
              a[x] = a[x] + total2;
          }
      }
      

      【讨论】:

        【解决方案3】:

        编写好的代码包括好的 cmets 和好的变量名选择。让我们首先逐行注释您的代码,以便您更好地了解发生了什么:

            public static int[] rowSums(int[][] arr2D){
        
                // A variable which is always 0
                int total2 = 0;
        
                // The actual output:
                int a[] = new int[arr2D.length];
        
                // For each row..
                for(int x=0; x<arr2D.length; x++){
        
                    // For each column..
                    for(int n=0; n<arr2D[x].length;n++){
        
                        // Put 0 into the 2D array (this line is backwards):
                        arr2D[x][n] = total2;
        
                        // Add the 'total' (always 0) into the current output
                        a[x] = a[x] + total2;
                    }
                }
        
                // Return the output
                return a;
            }
        

        Total2 从未设置

        好的,所以希望你的其中一行是向后的(并且你有一些糟糕的变量命名选择)会更清楚一点。更好的东西看起来更像这样:

            public static int[] rowSums(int[][] arr2D){
        
                // The actual output:
                int totals[] = new int[arr2D.length];
        
                // For each row..
                for(int row=0; row<arr2D.length; row++){
        
                    // For each column..
                    for(int col=0; col<arr2D[row].length;col++){
        
                        // Get the column value:
                        int columnValue = arr2D[row][col];
        
                        // Add the column amount into the total:
                        totals[row] = totals[row] + columnValue;
                    }
                }
        
                // Return the output
                return totals;
            }
        

        由于变量现在更加清晰,我们可以将多余的 cmets 删除为:

            public static int[] rowSums(int[][] arr2D){
        
                int totals[] = new int[arr2D.length];
        
                for(int row=0; row<arr2D.length; row++){
                    for(int col=0; col<arr2D[row].length;col++){
                        int columnValue = arr2D[row][col];
                        totals[row] = totals[row] + columnValue;
                    }
                }
        
                return totals;
            }
        

        【讨论】:

        • 有些人会争辩说好的代码甚至不需要 cmets :-)(也许​​ Javadoc 是可以的)。
        • @TimBiegeleisen 我完全同意——尽管对于刚开始的人来说,他们让一切变得更容易阅读(并且也有助于选择这些命名)。
        • 嗯,对我来说有点太冗长了
        • @ScaryWombat 由于 OP 存在初学者范围界定问题以及向后退行,我认为他们需要尽可能清晰才能真正理解他们的代码在做什么。
        • @LukeBriggs 还有一只橡皮鸭
        【解决方案4】:

        arr2D[x][n] = total2; // 你将 0 分配给 arr2D[x][n]

        将其更改为 total2= arr2D[x][n] ;

        它会起作用的!

        【讨论】:

        • @Luke 不,你不知道它每次都会为 total2 分配新值。请试试看!!
        猜你喜欢
        • 2013-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-02
        • 2013-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多