Pritam Bohra
Pritam Bohra

Reputation: 4339

IndexOutOfBoundsException even though the elements exist in the arraylist

I have 10001 elements in the ArrayList when I am trying to remove them one by one, I get IndexOutOfBoundsException exception midway i.e. after 5000 elements. Don't really know why.

  public static void main(String[] args) {
    List<Integer> numberArrayList = new ArrayList<>();
    List<Integer> numberLinkedList = new LinkedList<>();

    long start, end;

    start = System.currentTimeMillis();
    for (int i = 0; i < 10_001; i++) {
      numberArrayList.add(i);
    }
    end = System.currentTimeMillis();

    System.out.println("Time taken to insert 10_001 elements in ArrayList: " + (end - start));

    start = System.currentTimeMillis();
    for (int i = 0; i < 10_001; i++) {
      numberLinkedList.add(i);
    }
    end = System.currentTimeMillis();

    System.out.println("Time taken to insert 10_001 elements in LinkedList: " + (end - start));

    /* **************************************************************/
    start = System.currentTimeMillis();
    for (int i = 0; i < 10_001; i++) {
      numberArrayList.remove(i);
    }
    end = System.currentTimeMillis();

    System.out.println("Time taken to remove 10_001 elements from the front in ArrayList: " + (end - start));

    start = System.currentTimeMillis();
    for (int i = 0; i < 10_001; i++) {
      numberLinkedList.remove(i);
    }
    end = System.currentTimeMillis();

    System.out.println("Time taken to remove 10_001 elements from the front in LinkedList: " + (end - start));


  }

The above program results in

Time taken to insert 100_001 elements in ArrayList: 2
Time taken to insert 100_001 elements in LinkedList: 1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 5001, Size: 5000
    at java.util.ArrayList.rangeCheck(ArrayList.java:657)
    at java.util.ArrayList.remove(ArrayList.java:496)
    at collections_jnit.list.ArrayList_vs_LinkedList.main(ArrayList_vs_LinkedList.java:34)

Upvotes: 0

Views: 883

Answers (5)

Vilius Kukanauskas
Vilius Kukanauskas

Reputation: 112

numberLinkedList.clear();

or if you want to iterate, Use Java 8+ :)

numberLinkedList.removeIf(i -> true);

Upvotes: 0

SSC
SSC

Reputation: 3008

As already answered, why you are getting IndexOutOfBoundException, so I am going to add, how can you safely remove all the elements. There are two ways of doing this:

  1. ArrayList.clear()

  2. ArrayList.removeAll()

Both of the methods are inherited from List into ArrayList. 1 is faster than 2.

Upvotes: 1

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

Once you remove 5000 elements mean 10001-5000 = 5001 so considering the indexing from 0 so now range of index is 0 to 5000 hence the exception with 5001.

Solution : if you want to remove items from list while traversing then I recommend to use Iterators

Iterator<Integer> itr = numberArrayList.iterator();
while (itr.hasNext()) {
    itr.remove();
}

Upvotes: 3

Victor
Victor

Reputation: 8490

Lists are not Arrays. If you add an element to a List with size N the list size will be increased, that means you can access the element N + 1. If you remove any element, you cannot access the element N + 1, this element will be in the position N.

Arrays has fixed positions and fixed size.

Please, look the documentation:

Upvotes: 0

clinomaniac
clinomaniac

Reputation: 2218

You can change it to a while loop:

while (numberLinkedList.size() > 0) {
    numberLinkedList.remove(0);
}

With the for loop, you are removing the object at i which is increasing every iteration and thus you end up only removing half the items.

Upvotes: 1

Related Questions