简体   繁体   English

添加锯齿状二维数组的列

[英]Adding the columns of a jagged 2D array

I've looked all over for a good answer, but I was surprised I couldn't find one that accomplished quite what I'm trying to do.我四处寻找一个好的答案,但我很惊讶我找不到一个能完成我想要做的事情的答案。 I want to create a method that finds a columns sum in a jagged 2D array, regardless of the size, whether it's jagged, etc. Here is my code:我想创建一个在锯齿状二维数组中查找列总和的方法,无论大小,是否锯齿状等。这是我的代码:

public static int addColumn(int[][] arr, int x) {
    int columnSum = 0;
    int i = 0;
    int j = 0;
    int numRows = arr.length;
    //add column values to find sum

    while (i < numRows) {
        while (j < arr.length) {
            columnSum = columnSum + arr[i][x];
            j++;
            i++;
        }
    }//end while loop
    return columSum;
}

So, for example, consider I have the following array:因此,例如,考虑我有以下数组:

int[][] arr = {{10, 12, 3}, {4, 5, 6, 8}, {7, 8}};

I want to be able to pass x as 2 , and find the sum for Column 3 which would be 9 .我希望能够将x作为2传递,并找到Column 3的总和,即9 Or pass x as 3 to find Column 4 , which would simply be 8 .或者将x作为3来查找Column 4 ,它只是8 My code is flawed as I have it now, and I've tried about a hundred things to make it work.我的代码有缺陷,因为我现在拥有它,我已经尝试了大约一百件事来使它工作。 This is a homework assignment, so I'm looking for help to understand the logic.这是一个家庭作业,所以我正在寻找帮助来理解逻辑。 I of course keep getting an Out of Bounds Exception when I run the method.当我运行该方法时,我当然会不断收到越界异常。 I think I'm overthinking it at this point since I don't think this should be too complicated.我想我在这一点上想得太多了,因为我认为这不应该太复杂。 Can anyone help me out?谁能帮我吗?

I think that your second while loop is making your sum too big.我认为您的第二个while循环使您的总和太大。 You should only have to iterate over the rows.您应该只需要遍历行。

while (i < numRows) {
    if (x < arr[i].length) {
        columnSum += arr[i][x];
    }
    ++i;
}

I saw the codes above.我看到了上面的代码。 None of it is the adequate answer since posting here the code which meets the requirement.自从在此处发布满足要求的代码以来,这些都不是适当的答案。 Code might not look arranged or optimized just because of the lack of time.由于时间不够,代码看起来可能没有经过整理或优化。 Thanks.... :)谢谢.... :)

package LearningJava;

import java.util.Scanner;

public class JaggedSum {
    public static void main(String[] args) {
        int ik = 0;
        int ij = 0;
        Scanner scan = new Scanner(System.in);
        int p = 0;
        int q = 0;
        int[][] arr = new int[2][];
        System.out.println("Enter the value of column in Row 0 ");
        p = scan.nextInt();
        System.out.println("Enter the value of column in Row 1 ");
        q = scan.nextInt();
        arr[0] = new int[p];
        arr[1] = new int[q];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = scan.nextInt();
            }
        }
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
        if (arr[1].length > arr[0].length) {
            int[] columnSum = new int[arr[1].length];
            int x;
            for (x = 0; x < arr[0].length; x++) {
                columnSum[x] = arr[0][x] + arr[1][x];
                System.out.println(columnSum[x]);
            }
            ik = arr[0].length;
            for (int j = ik; j < arr[1].length; j++) {
                columnSum[j] = arr[1][j];
                System.out.println(columnSum[j]);
            }
        } else {
            int[] columnSum = new int[arr[0].length];
            int x;
            for (x = 0; x < arr[1].length; x++) {
                columnSum[x] = arr[0][x] + arr[1][x];
                System.out.println(columnSum[x]);
            }
            ij = arr[1].length;
            for (int j = ij; j < arr[0].length; j++) {
                columnSum[j] = arr[0][j];
                System.out.println(columnSum[j]);
            }
        }
    }
}

In Java 8 , you can use IntStream for this purpose:Java 8 中,您可以为此使用IntStream

public static int addColumn(int[][] arr, int x) {
    // negative column index is passed
    if (x < 0) return 0;
    // iteration over the rows of a 2d array
    return Arrays.stream(arr)
            // value in the column, if exists, otherwise 0
            .mapToInt(row -> x < row.length ? row[x] : 0)
            // sum of the values in the column
            .sum();
}
public static void main(String[] args) {
    int[][] arr = {{10, 12, 3}, {4, 5, 6, 8}, {7, 8}};
    System.out.println(addColumn(arr, 2)); // 9
    System.out.println(addColumn(arr, 3)); // 8
    System.out.println(addColumn(arr, 4)); // 0
    System.out.println(addColumn(arr, -1));// 0
}

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

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