简体   繁体   English

错误数组索引超出某些代码的范围

[英]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); 这行: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)); 返回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) . 目前尚不清楚您的代码在这种情况下打算做什么,因为您的伪代码未定义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. 另外,使用indexNumber表示一个错误,因为只有在其值为0时才使用-递增是毫无意义的。 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: 作为一种调试技术,我建议在函数调用开始时添加一些print语句:

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. 最后,一旦在一些简单的情况下可以使用,编写一个模糊测试器,在一堆随机列表和不同大小a s上检查您的算法,以查看是否遗漏了任何情况。

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

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