为什么一个循环抛出ConcurrentModificationException,而另一个则没有抛出?

为什么一个循环抛出ConcurrentModificationException,而另一个则没有抛出?,第1张

为什么一个循环抛出ConcurrentModificationException,而另一个则没有抛出?

正如其他人解释的那样,迭代器检测到对基础集合修改,这是一件好事,因为它可能会导致意外的行为。

想象一下下面的无迭代器代码可以修改集合:

for (int x = 0; list.size(); x++){  obj = list.get(x);  if (obj.isExpired())  {    list.remove(obj);    // Oops! list.get(x) now points to some other object so if I     // increase x again before checking that object I will have     // skipped one item in the list  }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5490340.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存