I use a for loop for each loop to iterate over PriorityQueueand try to poll it.
Why am I getting java.util.ConcurrentModificationException? Where can I change the " concurrently" as implied in the exception name? Is the assignment itself a modification since it internally uses poll ()? Explain, please.
Excerpt:
String[] sa = {">ff<", "> f<", ">f <", ">FF<"};
PriorityQueue<String> pq = new PriorityQueue<String>();
for (String str : sa) {
pq.offer(str);
}
System.out.println(pq);
for (String str : pq) {
System.out.print(pq.poll() + " ");
}
}
Edit:
As I understand from the explanations below, each call to the poll () method causes a resize of the object PriorityQueue. Thus, it makes sense to throw an exception when repeating and trying to poll ( varying the size constantly). Therefore, the object pqthrows an exception that is incorrect. Do I understand correctly?
user3034861