您的位置:首页 > 编程语言 > Java开发

遍历集合时出现的异常Exception in thread "main" java.util.ConcurrentModificationException

2014-04-17 21:57 791 查看


如果这样写的话,就会出现异常

public void operate(List list){
for (Iterator it = list.iterator(); it.hasNext();) {
String str = (String)it.next();
if (str.equals("-")){
list.remove(str);
}
}
}因为list在循环中的时候是不可以删除它的元素的

这样写就没有问题
for (Iterator it = list.iterator(); it.hasNext();) {
String str = (String)it.next();
if (str.equals("-")){
it.remove();
}
}

原文网址:http://harveyzeng.iteye.com/blog/1667091
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐