简体   繁体   中英

Error array index out of bounds in some code

There is an array index out of bounds exception in my recursive function. Can someone try to point out to me why that is the case? This line: return minChange(a, IntegerList); is having the array index out of bounds exception as well as most likely this line:

return minimumValue(1 + minChange(a - d, integerList), minChange(a, updatedList));

    /* Function minChange: Minimum Change
    Pseudo-code:
    minChange(0, ds)      = 0
    minChange(a, [])      = Failure
    minChange(a, d :: ds) = minChange(a,ds)                     if d > a
    minChange(a, d :: ds) = min(1 ++ minChange(a - d, d :: ds)  otherwise
     */
    public int minChange(int a, List<Integer> integerList) {

        //int minimumResult = 0;
        int indexNumber = 0;

        int d = integerList.get(indexNumber); (line 246)

        if(a == 0) {
            // If a is 0 return 0
            return 0;

        } else if(integerList.isEmpty()) {
            return -1;

        } else if(d > a) {

            integerList.remove(indexNumber); // Remove first element from list

            // Recursive call to minChange
            return minChange(a, integerList); (line 261)

        } else {
            // Create updatedList and remove first element
            List<Integer> updatedList = integerList;
            updatedList.remove(indexNumber);
            indexNumber++;

            return minimumValue(1 + minChange(a - d, integerList), minChange(a, updatedList)); (line 269)

        }


Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 0 out-of-bounds for length 0
at AlgorithmsSetZero.minChange(AlgorithmsSetZero.java:246)
    at AlgorithmsSetZero.minChange(AlgorithmsSetZero.java:261)
    at AlgorithmsSetZero.minChange(AlgorithmsSetZero.java:261)
    at AlgorithmsSetZero.minChange(AlgorithmsSetZero.java:261)
    at AlgorithmsSetZero.minChange(AlgorithmsSetZero.java:261)
    at AlgorithmsSetZero.minChange(AlgorithmsSetZero.java:261)
    at AlgorithmsSetZero.minChange(AlgorithmsSetZero.java:261)
    at AlgorithmsSetZero.minChange(AlgorithmsSetZero.java:261)
    at AlgorithmsSetZero.minChange(AlgorithmsSetZero.java:269)
    at AlgorithmsSetZero.minChange(AlgorithmsSetZero.java:269)
    at AlgorithmsSetZero.minChange(AlgorithmsSetZero.java:269)


How can I fix this array index out of bounds exception. It seems one line needs to be fixed. If so how can I fix this error? What are some ways?

Why it Fails:

A specific sequence of events that leads to the out of bounds exception would be, for example:

minChange(1, [4]) // goes into second-to-last (else if) case, since 4 > 1. 4 is then removed.
minChange(1, [])  // crashes

It's not clear what your code was intended to do in this case, since your pseudo-code doesn't define minChange(a, ds) .

Even if we stipulate that the input list has to have more than one item, we'll often hit this same case:

minChange(5,[4, 8])
minChange(1,[8])
minChange(1,[])

I'm not sure what you intended to happen here, but, anyway, there are other issues...

Bugs:

There is very likely a bug in your code with this line:

// this just creates another reference to the existing list
// https://stackoverflow.com/questions/6536094/java-arraylist-copy
// meaning: both recursive calls will operate on the same list
// even though one uses the "updatedList" reference and one uses "integerList"
List<Integer> updatedList = integerList;

Also, the use of indexNumber indicates a bug, since it's only ever used when it has value 0 - the incrementing is pointless. Remember that local variables in the function are getting 'reset' in each recursive call, unless you pass them as a parameter.

Debugging Strategies:

First, clarify what you actually want the algorithm to do.

Then, check what it's actually doing. As a debugging technique, I would recommend adding some print statement at the start of the function call:

System.out.println("minChange(" + a + ", " + integerList + ")");

...so that you can see a log of what happened before the crash. You can also use a debugger for this purpose.

Finally, once it works on some simple cases, write a fuzz tester that checks your algorithm on a bunch of random lists and a s of different sizes, to see if there are any cases that you missed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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