3

I am filling an ArrayList al with random integers. Then when I try to remove them i'm getting these errors:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 5000 out-of-bounds for length 5000 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.remove(ArrayList.java:517) at a2_40038465.ListTester.main(ListTester.java:137)

This is my code:

System.out.println("-----------------------------------------------------------------------------");
    System.out.println("Inserting at the beginning...");
    long startTime4 = System.currentTimeMillis();

    for(int i=0; i<10000; i++) {
        al.add(0, rand.nextInt(20001));
    }

    long endTime4 = System.currentTimeMillis();
    long elapsed4 = endTime4 - startTime4;
    System.out.println("Time elapsed: " + elapsed4);
    System.out.println("-----------------------------------------------------------------------------");

    System.out.println("Removing from beginnig...");
    long startTime19 = System.currentTimeMillis();

    for(int i=0; i<10000; i++) {
        al.remove(i);
    }

    long endTime19 = System.currentTimeMillis();
    long elapsed19 = endTime19 - startTime19;
    System.out.println("Time elapsed: " + elapsed19);
    System.out.println("-----------------------------------------------------------------------------");
luk2302
  • 55,258
  • 23
  • 97
  • 137
  • 1
    When i change it to `i<5000` it works. –  Oct 25 '18 at 17:37
  • 3
    `ArrayList`'s start at zero, so in a `List` of size 5000 the last index is 4999 – GBlodgett Oct 25 '18 at 17:39
  • If you want to remove from the beginning, use `remove(0)`, not `remove(i)`. Perhaps try debugging this to see what is happening, either with a debugger or on paper. – Andy Turner Oct 25 '18 at 17:39
  • 1
    Once you've removed the first 5000 elements, there are only 5000 left. So your index is now out of bounds. – Michael Oct 25 '18 at 17:40
  • yeah but i added 10,000 elements to the list, why is it limited to 5000? @GBlodgett –  Oct 25 '18 at 17:41
  • `a2_40038465.ListTester.main(ListTester.java:137)` This tells you that the error occurs on line 137 of ListTest.java. Start there. Then read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ for tips on how to debug your code. – Code-Apprentice Oct 25 '18 at 17:42
  • 1
    @Qwerty0987 "why is it limited to 5000?" Because you removed the first 5000 elements already. – Code-Apprentice Oct 25 '18 at 17:43
  • Thanks everyone for the quick help, i realized now my stupid mistake. –  Oct 25 '18 at 17:47

1 Answers1

5
al.remove(i);

removes the element at index i. In the beginning of the removal your list has 10000 items. After the first 5000 iterations of the loop your list only has 5000. Then you call al.remove(5000) which in an array of size 5000 is no longer allowed. In the step before you had 5001 elements in the list and called al.remove(4999) which was allowed.

Use remove(0) in the loop or clear() without any loop to remove all elements.

Your main mistake is probably thinking that remove(0) and a subsequent remove(1) removes the first 2 elements of the array. But that is not the case. What actually happens is that the first statement removes the first element, after that the originally second element becomes the first, the third one becomes the second, etc. So remove(1) after that removes the originally third elment. You would never remove the originally second element (and many others) if you continued that logic.

luk2302
  • 55,258
  • 23
  • 97
  • 137