简体   繁体   English

查找二维数组的行总和,并将其返回到数组中

[英]Finding the sum of the rows of a 2d array and returning it in an array

I'm trying to finish an AP CS FRQ question. 我正在尝试完成AP CS FRQ问题。 I wrote the code, but it doesn't work. 我写了代码,但是没有用。 Where have I messed up? 我在哪里搞砸了?

Write a static method rowSums that calculates the sums of each of the rows in a given twodimensional array and returns these sums in a one-dimensional array. 编写一个静态方法rowSums,它计算给定二维数组中每一行的总和,并以一维数组形式返回这些总和。 The method has one parameter, a twodimensional array arr2D of int values. 该方法具有一个参数,即int值的二维数组arr2D。 The array is in row-major order: arr2D[r][c] is the entry at row r and column c. 该数组按行顺序排列:arr2D [r] [c]是行r和列c的条目。 The method returns a one-dimensional array with one entry for each row of arr2D such that each entry is the sum of the corresponding row in arr2D. 该方法为arr2D的每一行返回一个带有一个条目的一维数组,以使每个条目都是arr2D中相应行的总和。 As a reminder, each row of a two-dimensional array is a one-dimensional array. 提醒一下,二维数组的每一行都是一维数组。

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

Your assignment is backwards, you should be storing each element of the 2D array using this: 您的分配是向后的,您应该使用以下命令存储2D数组的每个元素:

total2 = arr2D[x][n];

not this: 不是这个:

arr2D[x][n] = total2;

Full code: 完整代码:

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

You need to reset total2 within the outer loop, and set the value after the end of the inner loop 您需要在外部循环中重置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;
    }

If total2 is not going to be re-used this can be shortened to 如果不打算重新使用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];
    }
}

Writing good code includes good comments and good variable name choices. 编写好的代码包括好的注释和好的变量名选择。 Let's start by first just commenting through your code line by line so you can better see what's going on: 首先让我们开始逐行注释代码,以便您更好地了解发生了什么:

    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 is never set 从未设置Total2

Ok, so hopefully it's a little clearer that one of your lines is backwards (and you have some poor variable naming choices). 好的,希望您的其中一行倒退了(希望您有一些较差的变量命名选择)。 Something better looks more like this: 更好的东西看起来像这样:

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

As the variables are now much clearer, we can remove the redundant comments into just this: 由于变量现在更加清晰了,我们可以将多余的注释删除为:

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

arr2D[x][n] = total2; arr2D [x] [n] = total2; // you are assigning 0 to arr2D[x][n] //您将0分配给arr2D [x] [n]

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

and it will work!! 它会工作!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM