Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何计算二维数组中每列的总和?_Java_Arrays_Loops - Fatal编程技术网

Java 如何计算二维数组中每列的总和?

Java 如何计算二维数组中每列的总和?,java,arrays,loops,Java,Arrays,Loops,我正在编写一个程序,以便它计算并打印数组中每列的总和。给定的数据如下所示: int[][] data = {{3, 2, 5}, {1, 4, 4, 8, 13}, {9, 1, 0, 2}, {0, 2, 6, 3, -1, -8}}; 理想情况下,它应该输出结果13、9、15、13、12、-8。但由于某些行的长度不同,当我运行程序时,它会输出13、9、15,并给我一个ArrayIndexOutO

我正在编写一个程序,以便它计算并打印数组中每列的总和。给定的数据如下所示:

int[][] data = {{3, 2, 5},
                {1, 4, 4, 8, 13},
                {9, 1, 0, 2},
                {0, 2, 6, 3, -1, -8}};
理想情况下,它应该输出结果13、9、15、13、12、-8。但由于某些行的长度不同,当我运行程序时,它会输出13、9、15,并给我一个ArrayIndexOutOfBoundsException。我真的不知道如何修复它

这是我的密码:

public class ColumnSums
{
public static void main(String[] args) {

    //The given data
    int[][] data = {{3, 2, 5},
                    {1, 4, 4, 8, 13},
                    {9, 1, 0, 2},
                    {0, 2, 6, 3, -1, -8}};

    //Determine the number of data in the longest row
    int LongestRow = 0;
    for ( int row=0; row < data.length; row++){
        if ( data[row].length > LongestRow ){
            LongestRow = data[row].length;
        }
    }
    System.out.println("The longest row in the array contains " + LongestRow + " values"); //Testing

    //Save each row's length into a new array (columnTotal)
    int[] columnTotal = new int[4];

    //Scan through the original data again
    //Record each row's length into a new array (columnTotal)
    System.out.println("The lengths of each row are: ");
    for ( int i = 0; i < data.length; i++){
        columnTotal[i] = data[i].length;
        System.out.println(columnTotal[i]); //Testing
    }


    // Create an array to store all the sums of column
    int ColumnSums[] = new int[LongestRow];
    System.out.println("The sums of each column are: ");

    for ( int i = 0; i < LongestRow; i++ ){

            int sum = 0;

            for (int j = 0; j < data.length; j++) {
                    sum = sum + data[j][i];
            }

            ColumnSums[i] = sum;
            System.out.println("Column " + i + ": " + ColumnSums[i]); //Testing
    }


}
}
公共类列和
{
公共静态void main(字符串[]args){
//给定数据
int[]data={{3,2,5},
{1, 4, 4, 8, 13},
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8}};
//确定最长行中的数据数
int LongestRow=0;
for(int row=0;rowLongestRow){
LongestRow=数据[行]。长度;
}
}
System.out.println(“数组中最长的行包含”+LongestRow+“值”);//测试
//将每行的长度保存到新数组中(columnTotal)
int[]columnttotal=新int[4];
//再次扫描原始数据
//将每行的长度记录到新数组中(columnTotal)
System.out.println(“每行的长度为:”);
对于(int i=0;i

谢谢你的时间

要读取2D数组,循环应该是

for(int i = 0; i < array.length; ++i){
    for(int j = 0; j < array[i].length; ++j){
        System.out.println(array[i][j]);
    }
}
for(int i=0;i
请参阅使用
获取(int j=0;j
以使用当前行长度

这样,您将防止此
ArrayIndexOutOfBoundsException


如果你需要的话,我可以提问。只需发表评论

要读取2D数组,循环应该是

for(int i = 0; i < array.length; ++i){
    for(int j = 0; j < array[i].length; ++j){
        System.out.println(array[i][j]);
    }
}
for(int i=0;i
请参阅使用
获取(int j=0;j
以使用当前行长度

这样,您将防止此
ArrayIndexOutOfBoundsException


如果你需要的话,我可以提问。只需发表评论

基本上,您只需要在列中循环,直到每行的计数器超出边界。无需提前循环查找最长的行

   public static ArrayList<Integer> getCollumnSum() {
        int[][] data = {{3, 2, 5},
                        {1, 4, 4, 8, 13},
                        {9, 1, 0, 2},
                        {0, 2, 6, 3, -1, -8}};
        int col = 0;
        ArrayList<Integer> totals = new ArrayList<Integer>();
        while (true) {
          int total = 0;
          boolean dataInCol = false;
          for (int i = 0; i < data.length; i++) {
            if (col < data[i].length) {
                total += data[i][col];
                dataInCol = true;
            }
          }
          col += 1;
          if (dataInCol) {
            totals.add(total);
          } else {
            break;
          }
        }
        return totals;
      }

基本上,您只需要循环遍历列,直到每一行的计数器超出边界。无需提前循环查找最长的行

   public static ArrayList<Integer> getCollumnSum() {
        int[][] data = {{3, 2, 5},
                        {1, 4, 4, 8, 13},
                        {9, 1, 0, 2},
                        {0, 2, 6, 3, -1, -8}};
        int col = 0;
        ArrayList<Integer> totals = new ArrayList<Integer>();
        while (true) {
          int total = 0;
          boolean dataInCol = false;
          for (int i = 0; i < data.length; i++) {
            if (col < data[i].length) {
                total += data[i][col];
                dataInCol = true;
            }
          }
          col += 1;
          if (dataInCol) {
            totals.add(total);
          } else {
            break;
          }
        }
        return totals;
      }

我已经修改了你的代码以满足你的需要

int[][] data =  {{3, 2, 5},
                {1, 4, 4, 8, 13},
                {9, 1, 0, 2},
                {0, 2, 6, 3, -1, -8}};

int longestRow = 0;
for ( int row=0; row < data.length; row++){
    if ( data[row].length > longestRow ){
        longestRow = data[row].length;
    }
}
System.out.println("The longest row in the array contains " + longestRow + " values"); 
您无法在每个内部循环中获得每列的总和,因为每列的总和将在两个循环完成后获得。因此,变量
sum
是无用的。所以,它会像下面这样

int columnSums[] = new int[longestRow];
for ( int i = 0; i < data.length; i++ ){
        for (int j = 0; j < data[i].length; j++){
                columnSums[j] +=data[i][j];
            }
    }

我已经修改了你的代码以满足你的需要

int[][] data =  {{3, 2, 5},
                {1, 4, 4, 8, 13},
                {9, 1, 0, 2},
                {0, 2, 6, 3, -1, -8}};

int longestRow = 0;
for ( int row=0; row < data.length; row++){
    if ( data[row].length > longestRow ){
        longestRow = data[row].length;
    }
}
System.out.println("The longest row in the array contains " + longestRow + " values"); 
您无法在每个内部循环中获得每列的总和,因为每列的总和将在两个循环完成后获得。因此,变量
sum
是无用的。所以,它会像下面这样

int columnSums[] = new int[longestRow];
for ( int i = 0; i < data.length; i++ ){
        for (int j = 0; j < data[i].length; j++){
                columnSums[j] +=data[i][j];
            }
    }

这里的诀窍是获得特定行的长度(为了以防万一,您应该始终这样做)。因此,内部循环看起来像(intj=0;jlook这篇文章可能重复的可能重复的技巧是获取特定行的长度(你应该经常做的只是以防万一)。因此,内部循环看起来像(int j=0;j,看这篇文章的可能重复的可能重复的,我们不是应该提供一个解决方案来计算列的总和吗?首先,我没有注意到,第二,没关系,我不是来做作业的,而是来指导;)顺便说一句,你也是;)是的,但现在做一些相应的修改。:)@桑克特马卡尼没有。。。因此,我们不打算提供随时可用的代码,而是解释问题。。。仅仅提供一个解决方案对任何人都没有帮助(除了我和我的无聊…),我删除了代码,因为我发现它很容易编写,顺便问一下,我们不应该提供一个解决方案来计算列的总和吗?首先,我没有注意到,其次,没关系,我不是来做作业的,而是来指导;)顺便说一句,你也是;)是的,但现在做一些相应的修改。:)@桑克特马卡尼没有。。。因此,我们不打算提供随时可用的代码,而是解释问题。。。仅仅提供一个解决方案对任何人都没有帮助(除了我和我的无聊…),我删除了代码,因为我发现它很容易编写