简体   繁体   English

返回方法时的ArrayExceptionOutOfBounds

[英]ArrayExceptionOutOfBounds when returning the method

I am attempting to create a fillArray method that fills and array with 20 random values and sums every third value. 我试图创建一个fillArray方法,该方法用20个随机值填充和排列并求和第三个值。 I am getting an ArrayExceptionOutOfBounds at line 21 which is when the method is called. 我在第21行获得了ArrayExceptionOutOfBounds,这是调用该方法的时间。 Through the debug I have watched the array fill with proper values and the sum properly calculated. 通过调试,我观察到数组填充了正确的值并正确地计算了总和。 I am wondering what the error is. 我想知道错误是什么。

public static void fillArray(){
    //adding up A[0], A[3], A[6}, ...

    double[] A = new double[20];
    for(int i = 0; i < A.length; i++)
        A[i] = Math.random();

    double sum = 0;
    int k = 0;
    do{
        k += 3;
        sum += A[k];
    }while(k < 20);

    System.out.println("sum = " + sum);
}

Again I am looking to determine the reason for the error not necessarily a way to fix it. 我再次试图确定错误的原因,而不一定是解决它的方法。

Here is your problem: 这是您的问题:

do{
    k += 3;
    sum += A[k];
}while(k < 20);

K will be equal to 0, then 3, then 6, etc, up until it reaches 21 and then you try and access A[21] which is out of bounds. K将等于0,然后是3,然后是6,依此类推,直到达到21,然后您尝试访问超出范围的A [21]。

This is because when k = 18 on the 6th iteration of the while loop, (k < 20) is true and therefore the while loop continues and adds another 3 to k making it 21. After that, the while loop stops as k is not less than 20 anymore leaving k with a value of 21. 这是因为在while循环的第6次迭代中,当k = 18时(k < 20)为真,因此while循环继续进行,并在k上加上了另外3个使其成为21。此后,while循环停止了,因为k不是小于20,则k的值为21。

You're getting the error because you're hitting 21 on a array with the size of 20. To fix: 之所以会出现错误,是因为您在一个大小为20的数组上命中了21。

 do{
    k += 3;
    if(k <= 20){
       sum += A[k];
   }
}while(k < 20);

Your second loop, where you calculate the sum, is a do/while loop, meaning the condition will always get checked after the loop body is executed. 计算总和的第二个循环是一个do / while循环,这意味着在循环体执行始终会检查条件。 You count k up in steps of 3, meaning it will reach 21 at some point before the while (k < 20) condition returns false, resulting in your error. 你算k中的3个步骤了,这意味着它会在之前的一些点达到21 while (k < 20)条件返回false,导致你的错误。

I think the problem is that k is incrementing and being used as an array index, before the <20 test. 我认为问题在于,在<20测试之前,k正在递增并被用作数组索引。

something like this might work better: 这样的事情可能会更好:

for (int k = 0; k < 20; k = k + 3) {
        sum += A[k];
    }

In general, i think the do while construct here is a bit of unnecessary complexity. 通常,我认为这里的do while构造有点不必要的复杂性。 The version above is easier to read and is a more common pattern. 上面的版本更易于阅读,是更常见的模式。

With the current logic, k will be 3,6,9,12,15,18 and 21. The last value is responsible for the out of bounds exception. 在当前逻辑下,k将为3、6、9、12、15、18和21。最后一个值是导致越界异常的原因。 Your loop will not stop at 18 as it is smaller than 20. You could resolve the bug by changing your code to this: 您的循环不会停止在18处,因为它小于20。您可以通过将代码更改为此来解决该错误:

do{
    k += 3;
    sum += A[k];
}while(k < 18);

You increment the array index before using it. 您需要先增加数组索引,然后再使用它。 So you are not only going beyond the array index in last pass, you are also not adding the element at 0 index. 因此,您不仅在上一遍超越了数组索引,而且还没有在0索引处添加元素。

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

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