首页 文章

查找2d数组的行总和并将其返回到数组中

提问于
浏览
0

我正在尝试完成一个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;
    }`

4 回答

  • 0

    您的任务是向后的,您应该使用以下方法存储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;
        }
    }
    
  • 0

    您需要在外部循环中重置 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];
        }
    }
    
  • -1

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

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

    它会工作!!

  • 0

    编写好的代码包括良好的注释和良好的变量名称选择 . 让我们首先开始逐行评论您的代码,这样您就可以更好地了解正在发生的事情:

    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;
        }
    

    由于变量现在更加清晰,我们可以将冗余注释删除为:

    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;
        }
    

相关问题