Is it possible to iterate through a LL in Java using a ListIterator, add objects periodically to the list, and process these items in the list in the order they were added?
Let's say I start with a LL with a single object in it. I process this object, and decide I want to add two additional objects, which I want to further process (like FIFO). Intuitively, I start the process with
while (itr.hasNext()) {
itr.next();
...
itr.add();
}
However, this seems to quickly crumble - add actually adds items BEFORE the index I currently am at, and not after (ListIterator javadoc). This means when I hit the while loop start again, it actually doesn't recognize that stuff was added to the LL, because it actually needs to go BACKWARDS (.hasPrevious()
instead of .hasNext()
) to find it. But I can't start the LL with .hasPrevious()
(i don't think) because the first item in the LL is a .next()
item.
How does one cleanly do this? Or am i just being stupid?