java - Modifying a list inside iteration -
we know illegal , throw concurrentmodificationexception
:
for (item : thelist) { if (i.num == 123) foo(i); // foo modifies thelist }
but this?
for (item : thelist) { if (i.num == 123) { foo(i); // foo modifies thelist break; } }
because loop broken before thelists
's iterator's next
called, there no concurrentmodificationexception
. make legal?
after thinking more, concluded has be. "solution" be
for (item : thelist) { if (i.num == 123) { thei = i; break; } } foo(thei); // foo modifies thelist
but in terms of how next
called, that's same.
Comments
Post a Comment