簡體   English   中英

計算不均勻二維數組中的列總和

[英]Calculating sum of columns in uneven 2D array

我一直在通過一些免費的在線教程自學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++;
                    }   
                }
            }

如果您嘗試計算行總和

好像有一個循環太多了。 您的while遍歷了第一個維度,而for(row...是第二個維度。因此,無需通過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]);
        }
    }
}

如果我正確理解您的問題,並且想要第一,第二,第三等列的總和,則可以嘗試以下操作。

// 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...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM