Re: what happen in for (x:List) and iterator when adding to List



Lew <noone@xxxxxxxxxxxxx> wrote:
for (ListIterator<E> it = list.listIterator(); it.hasNext(); )
{....; list.add(x);
}
Where does 'x' come from?

Probably it doesn't matter at all. It's just some element that's
to be added to end of the currently iterated list.
It doesn't work that way, of course. I understood the question not
so much as about the "why", but rather about "how else can I achieve
such an effect".

for (x:list){
.....; list.add(x);
}
Wouldn't that make the List grow until it hits OutOfMemoryError (OOME)?
(Isn't that the point of Andreas' response?)

No that wasn't it. I (boldly) assumed, that the ".....;" contained
something like: "if (x.exhausted()) continue; x.exhaustGradually();" :-)

Anyway, I meanwhile noticed, that my code is broken as well, because
I re-iterate also those elements that were originally in list...

.