您的位置:首页 > 其它

迭代集合删除问题

2008-01-26 08:47 246 查看
迭代出错:在循环删除一个集合的数据时,不能用Collection .remove
得用Iterator .remove才行
删除对象后,集合会重新排序,会导致发生异常

正确写法:
String str = null;
while (it.hasNext()) {
str = (String) it.next();
if (deliverySet.contains(str)) {
it.remove();
} else {
tempMap.put(str, "1"); }
}
以下写法就会出现异常情况:
String str = null;
while (it.hasNext()) {
str = (String) it.next();
if (deliverySet.contains(str)) {
deliverySet.remove(str);
} else {
tempMap.put(str, "1");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: