私たちは皆、これが違法であることを知っており、次をスローしConcurrentModificationException
ます。
for (Item i : theList) {
if (i.num == 123)
foo(i); // foo modifies theList
}
しかし、これはどうですか?
for (Item i : theList) {
if (i.num == 123) {
foo(i); // foo modifies theList
break;
}
}
theLists
の iteratornext
が呼び出される前にループが壊れているため、はありませんConcurrentModificationException
。しかし、それは合法になりますか?