在java中添加锯齿状2D数组的列

时间:2022-07-08 21:31:13

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:

我一直都在寻找一个好的答案,但我很惊讶我找不到一个完成了我想做的事。我想创建一个方法,在一个锯齿状的2D数组中查找列和,无论大小,是否是锯齿状等等。这是我的代码:

    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. Or pass x as 3 to find Column 4, which would simply be 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? Thanks a lot!

我希望能够将x传递为2,并找到第3列的总和为9.或者将x传递给3以找到第4列,这将只是8。我的代码现在有了瑕疵,并且我已经尝试了大约一百件事来使其发挥作用。这是一项家庭作业,所以我正在寻求帮助来理解逻辑。当我运行该方法时,我当然会得到一个Out of Bounds Exception。我想我此刻正在过度思考,因为我认为这不应该太复杂。谁能帮我吗?非常感谢!

2 个解决方案

#1


1  

I think that your second while loop is making your sum too big. You should only have to iterate over the rows.

我认为你的第二个while循环让你的总和太大了。您应该只需遍历行。

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

  ++i;
}

#2


0  

//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]);
              }
    }
    }
}

#1


1  

I think that your second while loop is making your sum too big. You should only have to iterate over the rows.

我认为你的第二个while循环让你的总和太大了。您应该只需遍历行。

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

  ++i;
}

#2


0  

//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]);
              }
    }
    }
}