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

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -