Im trying to add to a ListIterator while iterating over it but when checking if it worked hasNext() always returns false.
As javadoc states that a ListIterator is "An iterator for lists that allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the iterator's current position in the list." however when Im trying to add I'm neither getting an Error message nor is the ListIterator getting a new Element.
public void foo() {
List<String> bar = new ArrayList<String>();
bar.add("foobar");
ListIterator<String> quux = bar.listIterator();
while(quux.hasNext()) {
if(quux.next().equals("foobar")) {
quux.add("baz");
}
}
}
At the end of this funtion I expect the ListIterator to contain the new String "baz". However when I call the hasNext() method it returns false. The program even got past the if-Statement it just turns out, that the add-method doesnt do what its supposed to do.