简体   繁体   English

循环遍历For循环,ArrayIndexOutOfBounds

[英]Iterating Through For Loop, ArrayIndexOutOfBounds

Tried changing around the for loop condition several times, still get ArrayIndexOutOfBounds when I pass zero as a parameter. 尝试多次围绕for循环条件进行更改,但当我将零作为参数传递时仍然获得ArrayIndexOutOfBounds。 Every other number works fine, I am trying to account for zero by setting it equal to zero automatically, am I doing that part incorrectly? 每隔一个数字都可以正常工作,我试图通过自动将其设置为零来说明零,我做错了那部分吗? Everything compiles and runs fine except for zero. 除零外,所有内容均可编译并正常运行。

private static int iterativeCalculation(int userEntry)
    {

        int iterativeArray[] = new int[userEntry + 1];
        iterativeArray[0] = 0;
        iterativeArray[1] = 1;

        for (int i = 2; i <= userEntry; i++)
        {
          iterativeArray[i] = (3 * iterativeArray[i - 1]) - (2 * iterativeArray[i - 2]);
          iterativeEfficiencyCounter++;  
        } 
          return iterativeArray[userEntry];

    }

public static void main(String[] args) {

        System.out.println(iterativeCalculation(0));
}

Tried debugging my way through the code, still not understanding what is going wrong. 我尝试通过代码进行调试,但仍然不了解出了什么问题。 Would appreciate any help! 将不胜感激! Thanks! 谢谢!

When you pass zero as parameter, userEntry + 1 = 1. 当您将零作为参数传递时, userEntry + 1 = 1。

But here: 但在这儿:

    iterativeArray[1] = 1;

You are trying to set the second element's value. 您正在尝试设置第二个元素的值。 Remember that length of array is one less than its actual size. 请记住,数组的length比其实际大小小一。 So removing this line will fix it. 因此,删除此行将解决此问题。 Or use userEntry + 2 instead and alter your loop accordingly. 或者改用userEntry + 2并相应地更改循环。

EDIT: 编辑:

If you really want to fix first and second element, then use this instead: 如果您确实要修复第一个和第二个元素,请改用此方法:

int iterativeArray[] = new int[userEntry + 2];
iterativeArray[0] = 0;
iterativeArray[1] = 1;

This will create an array of adequate base size. 这将创建足够的基本大小的数组。

And remember, length you enter in [...] while creating array has to be one more than the actual length you want . 请记住, length您在输入[...]在创建数组必须比你想要的实际长度多一个 Because actual array starts counting from 0. 因为实际数组从0开始计数。

In your case, you were setting length as 1 (minimum). 在您的情况下,您将长度设置为1(最小)。 That would create an array which can store only one element; 那将创建一个只能存储一个元素的数组。 that is iterativeArray[0] = //something . 那是iterativeArray[0] = //something Anything above that is OutOfBounds . 上面的任何东西都是OutOfBounds

You are setting iterativeArray[1] = 1; 您正在设置iterativeArray[1] = 1; regardless of whether or not there are actually 2 or more items in the array. 无论数组中实际上是否有2个或更多项目。 That will be out of bounds with one element. 这将超出一个元素的范围。

I think you should step through the code in debugger to best understand what the problem is. 我认为您应该逐步调试程序中的代码以最好地了解问题所在。 You'll see exactly where it's got a problem if you single-step through the code. 如果您单步执行代码,则将确切看到问题出在哪里。 This is a fundamental technique and tool. 这是一项基本技术和工具。

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

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