/ Java

Adding new elements to arraylist in java while using iterator

Today i had a problem with usage of arraylist along side of iterator. the problem was that i was unable to add elements to the arraylist. it gives you an error java.util.ConcurrentModificationException this is because u cant add elements to an arraylist while using an iterator. so i had used listiterator as below.

import java.util.ArrayList;
import java.util.ListIterator;
public class Test { 
    public static void main(String args[]) { 
        ArrayList array_test= new ArrayList(); 
        array_test.add("a"); 
        array_test.add("k"); 
        array_test.add("d"); 
        array_test.add("s"); 
        array_test.remove("d"); 
        int i=1; 
        for(ListIterator it=array_test.listIterator();it.hasNext();) {
             String link=it.next(); 
            if(i==1) {
             it.add("r"); 
             it.previous(); 
             it.add("kk"); 
             it.previous(); 
             i++;
            } 
            System.out.println(link); 
        } 
    System.out.println("Contents of arrays list "+array_test); 
    } 
}

In the above code i had used it.previous whenever i used it.add. this is because i want to use the added value in the loop again. while the addition of new element in array is added just before next element in arraylist.

Prashanth Madi

Prashanth Madi

I'm a programmer & Tech enthusiast. I work for OpenSource Support Team at Microsoft, but this blog, its content & opinions are my own. I blog about tech, gadgets, code, where we're going & we've been.

Read More