I wrote a simple code for LinkedList, to add data into it and iterate over it,
I imported the required packages for LinkedList and Iterator,
There is no error in the Code,
I am getting the following Exception -
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
at java.util.LinkedList$ListItr.next(Unknown Source)
at collectionFramework.Demo.displayList(LinkedListDemo.java:38)
at collectionFramework.LinkedListDemo.main(LinkedListDemo.java:13)
Here's my code -
package collectionFramework;
import java.util.Iterator;
import java.util.LinkedList;
public class LinkedListDemo{
public static void main(String[] args) {
Demo obj = new Demo();
obj.addToList();
obj.displayList();
}
}
class Demo{
LinkedList<String> al;
Iterator<String> itr;
Demo(){
al = new LinkedList<String>();
itr = al.iterator();
}
void addToList(){
al.add("Aniruddha");
al.add("Hitesh");
al.add("Rahul");
al.add("Kshitij");
}
void displayList(){
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}