[Java]Queue in while loop, cannot modify the value?
- by javaLearner.java
This is my code:
Iterator it = queue.iterator();
while(it.hasNext()){
random = randNumber(1,2);
if(random == 1){
queue.poll();
} else {
queue.add("new");
queue.poll();
}
}
It gives me:
Exception in thread "test" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:761)
at java.util.LinkedList$ListItr.next(LinkedList.java:696)
Edit @Jon Skeet:
What I want to do is:
I have a queue list in, let say the size is 10, lets say: a,b,c,d ... j
Generate a number between 1 and 2. if 1, pull (remove the top element) else if 2 add new element
I will stop the loop until I added 3 new elements