
正如其他人解释的那样,迭代器检测到对基础集合的修改,这是一件好事,因为它可能会导致意外的行为。
想象一下下面的无迭代器代码可以修改集合:
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 }}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)