在一个2维阵列的列的总和 [英] sum of columns in a 2 dimensional array

查看:132
本文介绍了在一个2维阵列的列的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 静态双重[] [] initialArray = {{7.432,8.541,23.398,3.981},{721.859,6.9211,29.7505,53.6483},{87.901,455.72,91.567,57.988} };市民双[] columnSum(双[] []数组){
    INT索引= 0;
    双温[] =新的双[数组[索引]。长度]    的for(int i = 0; I<阵[I]。长度;我++){
        双总和= 0;        对于(INT J = 0; J< array.length; J ++){
            总和+ =阵列[J] [I]        }
        临时[指数] = SUM;
        的System.out.println(索引是:+指数+总和是:+总和);
        指数++;    }    返回温度;
}
公共静态无效的主要(字串[] args){
    arrayq测试=新arrayq();
    test.columnSum(initialArray);}

我想获得的所有列的总和,但我不断收到outofbounds例外。这是输出我得到:

 指数:0合计为:817.192
指数:1总和为:471.18210000000005
指数:2总和为:144.7155
异常线程mainjava.lang.ArrayIndexOutOfBoundsException:3
在NewExam.arrayq.columnSum(arrayq.java:11)


解决方案

您对循环条件外是给你的问题​​。这是你的循环: -

 的for(int i = 0; I<阵[I]。长度;我+ +)

现在,当 I 达到 3 ,您试图访问值阵列[3]。长度。这将抛出你 IndexOutOfBounds 例外。


由于每个内部数组的大小都是一样的,你可以的更改循环 -

 的for(int i = 0; I<阵[0]。长度;我+ +)

,或者甚至更好,只存储数组[0]。长度在手之前的一些变量。但是,这不会有太大的差别。


我也建议你使用一个更好的方式来计算列的总和。避免遍历排第一。保持迭代一个正常的大概是这样的: -

 公共双[] columnSum(双[] []数组){    INT大小=阵列[0]。长度; //与最大长度内数组的大小,将其更换
    双温[] =新的双[尺寸]    的for(int i = 0; I< array.length,我++){
        对于(INT J = 0; J<阵[I]。长度; J ++){
            温度[J] + =阵列[I] [J]。 //注意,我加入'临时[J]`。
        }
    }    的System.out.println(Arrays.toString(临时));
    返回温度; //注意你是不是在调用的方法使用该返回值
}

所以,你可以看到你的问题是如何高度简化。我做了什么,而不是值分配到阵列中,我加入数组[I] [J] 温度[J] 。于是,渐渐地,数组的值[I] [J] 所有我的(行)获取总结温度[J] 。这样,您就不必使用混乱的迭代。所以,只要添加上述code到你的方法,并删除旧的。

这个方法也将正常工作,即使你有锯齿形阵列,即你内心数组的大小不同。但是,只记得要仔细定义温度数组的大小。

另外请注意,我已经使用 Arrays.toString(临时)方法来打印数组。

static double [][] initialArray = {{7.432, 8.541, 23.398, 3.981}, {721.859, 6.9211, 29.7505, 53.6483}, {87.901, 455.72, 91.567, 57.988}};

public double[] columnSum(double [][] array){
    int index = 0;
    double temp[] = new double[array[index].length];

    for (int i = 0; i < array[i].length; i++){
        double sum = 0;

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

        }
        temp[index] = sum;
        System.out.println("Index is: " + index + " Sum is: "+sum);
        index++;

    }

    return temp;
}


public static void main(String[] args) {
    arrayq test = new arrayq();
    test.columnSum(initialArray);

}

I want to get the sum of all the columns, but I keep getting an outofbounds exception. This is the output I get:

Index is: 0 Sum is: 817.192
Index is: 1 Sum is: 471.18210000000005
Index is: 2 Sum is: 144.7155
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at NewExam.arrayq.columnSum(arrayq.java:11)

解决方案

Your outer for loop condition is giving you problems. Here's your loop: -

for (int i = 0; i < array[i].length; i++)

Now, when i reaches the value 3, you are trying to access array[3].length. This will throw you IndexOutOfBounds exception.


Since the size of every internal arrays are same, you can change your loop to: -

for (int i = 0; i < array[0].length; i++)

Or, even better, just store the array[0].length in some variable before hand. But that will not make much of a difference.


I would also suggest you to use a better way to calculate the sum of columns. Avoid iterating over rows first. Keep the iteration a normal one probably like this: -

public double[] columnSum(double [][] array){

    int size = array[0].length; // Replace it with the size of maximum length inner array
    double temp[] = new double[size];

    for (int i = 0; i < array.length; i++){
        for (int j = 0; j < array[i].length; j++){
            temp[j] += array[i][j];  // Note that, I am adding to `temp[j]`.
        }
    }

    System.out.println(Arrays.toString(temp));
    return temp;  // Note you are not using this return value in the calling method
}

So, you can see that how your problem is highly simplified. What I did is, rather than assigning the value to the array, I added the new value of array[i][j] to the existing value of temp[j]. So, gradually, the value of array[i][j] for all i's (rows) gets summed up in temp[j]. This way you don't have to use confusing iteration. So, just add the above code to your method, and remove the old one.

This method will also work fine, even if you have jagged-array, i.e., you inner arrays are not of same size. But just remember to define the size of temp array carefully.

Also note that, I have used Arrays.toString(temp) method to print the array.

这篇关于在一个2维阵列的列的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆