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("-----------------------------------------------------------------------------");