I want to implement an iterator for a circular linked list but i can't find how to imlement the ConcurrentModificationException when an user is trying to modify an iterator that is currently iterating.
I know i have to change the next and hasNext functions but i don't know how to do it.
I tried searching for java doc source code of iterator in order to help me understanding this but all i could find was "source code" with function names and documentation on how to use them ...
public Iterator<Item> iterator() {
return new ListIterator();
}
// an iterator, doesn't implement remove() since it's optional
private class ListIterator implements Iterator<Item> {
private Node current = last.next; //initialize as the first node
public boolean hasNext(){
return current == null;
}
public Item next(){
if(hasNext() == false) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
public void remove(){
throw new UnsupportedOperationException();
}
}