I am working on an application which has some legacy code. Here, there is a linkedlist and the code iterates that linklist using an iterator in a while loop.
LinkedList ll = grammarSection.getSectionsAsLinkList();
Iterator iter = ll.iterator();
int i=0;
while (iter.hasNext()) {
1. GrammarSection agrammarSection = (GrammarSection) iter.next();
2. grammarLineWithMatches = m_grammarLineMatcher.getMatch(agrammarSection, p_line);
3. if (grammarLineWithMatches != null) { //condition a
4. if (getPeek(ll)!=agrammarSection)
5. ll.addFirst(ll.remove(i)); //changing the linkedlist Line5
return grammarLineWithMatches;
}
i++;
}
In the while loop, if condition a is true, then the linkedlist is modified as in line5. However, in this case, the next method on line1 throws a ConcurrentModificationException. How to add and delete the linkedlist without getting any ConcurrentModificationException