【问题标题】:IndexOutOfBounds: Why does low keep going past high [closed]IndexOutOfBounds:为什么低点一直超过高点[关闭]
【发布时间】:2020-03-18 00:25:00
【问题描述】:

我不断收到下面的 if 语句的 IndexOutOfBounds 错误,我不知道为什么。 low 初始为 0,high 设置为 24,ArrayList 的大小为 25。

        for(int i = low + 1; low <= high; i++){
                if(list.get(i).compareTo(list.get(pivIndex)) < 0){ //this line
                E temp = list.get(pivIndex);
                list.remove(pivIndex);
                list.add(pivIndex, list.get(i));

                list.remove(i);
                list.add(i, temp);
            }
        }

【问题讨论】:

  • 你没有绑定i。您的 for 循环条件仅检查 low &lt;= high,尽管这些变量都没有在您的循环内更新。与此同时,i 可以随心所欲地变大。
  • 过去一天我一直在盯着这个...非常感谢

标签: java for-loop arraylist indexoutofboundsexception quicksort


【解决方案1】:

你没有绑定i。您的 for 循环条件为 low &lt;= high,但这些变量都没有在循环中更新。同时,i 变大,直到太大,这会导致异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多