计算不均匀2D阵列中的列总和

时间:2014-07-01 19:48:48

标签: java arrays loops

我通过一些免费的在线教程自学Java,并挑战自己完成所有的练习练习。我已经被困在这一个星期一个星期了,这让我发疯了。我感觉自己非常接近,只是因为组成不均匀柱子的不同阵列长度而绊倒。

            public class Testing {
                public static void main(String[] args) {
                    int[][] data = { {3, 2, 5},
                                     {1, 4, 4, 8, 13},
                                     {9, 1, 0, 2},
                                     {0, 2, 6, 3, -1, -8} };

                    int sum = 0;
                    int row = 0;
                    int col = 0;
                    int currentRow = 0;

                    while (currentRow < data.length) {
                        for (col = 0; col < data[currentRow].length; col++) {
                            sum = 0;
                            for (row = 0; row < data.length; row++) {
                                System.out.println(data[row][col]);
                                sum += data[row][col];
                            }
                            System.out.println("Sum: " + sum);
                        } 
                        currentRow++;
                    }   
                }
            }

2 个答案:

答案 0 :(得分:4)

如果您尝试计算行总和

似乎有一个循环太多了。您的while遍历第一维,for(row...遍历第二维。因此,for(row...无需再次浏览数据 尝试使用一个循环,如:

       public class Testing {
            public static void main(String[] args) {
                int[][] data = { {3, 2, 5},
                                 {1, 4, 4, 8, 13},
                                 {9, 1, 0, 2},
                                 {0, 2, 6, 3, -1, -8} };

                int sum = 0;

                for (int currentRow = 0; currentRow < data.length; currentRow++) {
                    sum = 0;
                    for (int col = 0; col < data[currentRow].length; col++) {
                        System.out.println(data[currentRow][col]);
                        sum += data[currentRow][col];
                    } 
                    System.out.println("Sum: " + sum);
                }   
            }
        }

如果您尝试计算列总和

public class Testing {
    public static void main(String[] args) {
        int[][] data = { {3, 2, 5},
                         {1, 4, 4, 8, 13},
                         {9, 1, 0, 2},
                         {0, 2, 6, 3, -1, -8} };
            // get max length of a row == number of columns
        int length = 0;
        for (int r = 0; r < data.length; r++) {
            int currLength = data[r].length;
            if(currLength>length) length = currLength;
        }
            // create array for column sums
        int[] sums = new int[length];
            // fill array with zeros
        Arrays.fill(sums, 0);
            // sum up
        for (int currentRow = 0; currentRow < data.length; currentRow++) {
            for (int col = 0; col < data[currentRow].length; col++) {
                System.out.println(data[currentRow][col]);
                sums[col] += data[currentRow][col];
            } 
        }   
            // print sums
        for (int i = 0; i < sums.length; i++) {
            System.out.println(i + ": " + sums[i]);
        }
    }
}

答案 1 :(得分:0)

如果我正确理解你的问题,并且你想要第一,第二,第三等列的总和,那么你可以尝试以下。

// array for the sums of the columns
int[] colSums;
int largestSize = 0;

// loop through and find the largest array of values (in this example its the 4th)
for(int x = 0; x < data.length; x++) {
    largestSize = Math.max(largestSize,data[x].length);
}

colSums = new int[largestSize]; // create the int array with the right length

// loop through the data array and add sums to the colSums array
for(int x = 0; x < data.length; x++) {

    colSums[x] = 0; // so that its not null when we add to it

    for(int y = 0; y < data[x].length; y++) {

        // here I add the number to the solSums array
        colSums[y] += data[x][y];
    }
}

// now colSums[0] will be the sum of the first column, colSums[1] = sum of second, etc...
相关问题