0

Tried changing around the for loop condition several times, still get ArrayIndexOutOfBounds when I pass zero as a parameter. 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!

1
  • 1
    You should set some boundary conditions on userEntry Commented Nov 30, 2015 at 1:52

3 Answers 3

3

When you pass zero as parameter, 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. So removing this line will fix it. Or use userEntry + 2 instead and alter your loop accordingly.

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. Because actual array starts counting from 0.

In your case, you were setting length as 1 (minimum). That would create an array which can store only one element; that is iterativeArray[0] = //something. Anything above that is OutOfBounds.

Sign up to request clarification or add additional context in comments.

2 Comments

But it doesn't fix it. I have to account for a user entry of 0 or 1 as a base case due to the equation. Removing that line still gives me an OutOfBoundsException. The idea is the first element will always be 0, the second element will always be 1.
If you really want to fix first and second elements (or zeroth and first in reality), then use int iterativeArray[] = new int[userEntry + 2]; instead. This will create an array of adequate base size.
0

You are setting iterativeArray[1] = 1; regardless of whether or not there are actually 2 or more items in the array. That will be out of bounds with one element.

2 Comments

Thanks for the response. Can you elaborate a little please? The idea was that the first entry in the array will always be zero and the second will always be one due to the equation, so I'm accounting for those. How should I change that code?
With an input of zero, you create an array of size 1 which only has one element: iterativeArray[0]. Therefore you cannot access iterativeArray[1] which you do in the function regardless of the size.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.