-3

I keep getting an IndexOutOfBounds error for the if statement down below and I don't know why. low is initially 0, high is set to 24, and the size of the ArrayList is 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);
            }
        }
cyz2km01
  • 3
  • 2
  • 3
    You're not bounding `i`. Your for-loop condition just checks `low <= high`, though neither of those variables is being updated inside your loop. Meanwhile `i` can get as large as it wants. – khelwood Nov 22 '19 at 00:13
  • i've been staring at this for the past day... Thank you so much – cyz2km01 Nov 22 '19 at 00:23

1 Answers1

0

You are not bounding i. Your for-loop condition says low <= high, but neither of those variables is being updated in the loop. Meanwhile, i gets larger until it's too big, and that causes the exception.

khelwood
  • 55,782
  • 14
  • 81
  • 108