java 遍历list、set时 删除元素 java.util.ConcurrentModificationException
来源:互联网 发布:专业淘宝团队 编辑:程序博客网 时间:2024/05/17 07:13
1,一般情况下,可使用 iterator 在遍历list、set的同时删除其中的某些元素,例如:
Iterator<String> it = collection.iterator();while (it.hasNext()) {
String str = it.next();
if (......) {
it.remove();
// System.out.print(collection);
}
}
2,而 ~~ 有递归时,如下:
对于List:
public void recursiveFunc(ArrayList<String> wordList) {
System.out.println(wordList);
Iterator<String> it = wordList.iterator();
while (it.hasNext()) {
String s = it.next();
if(......){
it.remove();
recursiveFunc(wordList); //把删除某元素之后的list 当参数传进行递归时,没有任何问题
}
}
}
对于Set:
System.out.print(set);
Iterator<String> it = set.iterator();
while (it.hasNext()) {
String s = it.next();
if(......){
it.remove();
recursiveFunc(set); // 把删除某元素之后的 set当参数传进行递归时,就会报错ConcurrentModificationException
}
}
}
——需要注意一下,
解决方法:new一个set,把remove()某元素之后的数据集copy到new出来的set中当 递归 参数。
0 0
- java 遍历list、set时 删除元素 java.util.ConcurrentModificationException
- java set 删除元素 java.util.ConcurrentModificationException
- JAVA List 一边遍历一边删除元素,报java.util.ConcurrentModificationException异常
- 遍历集合时删除元素,抛出java.util.ConcurrentModificationException的解决办法
- java.util.ConcurrentModificationException---用迭代器遍历Map时删除其中元素出现异
- JAVA集合中删除元素时的java.util.ConcurrentModificationException
- java 遍历 删除元素 List Set
- java 遍历 删除元素 List Set
- List java.util.ConcurrentModificationException
- java遍历集合删除特定元素的 java.util.ConcurrentModificationException异常
- ArrayList循环遍历并删除元素时报java.util.ConcurrentModificationException异常
- 集合遍历 java.util.ConcurrentModificationException
- Java 循环遍历删除set list中的元素
- for循环遍历删除报错:java.util.ConcurrentModificationException
- List删除元素报Exception in thread "main" java.util.ConcurrentModificationException异常,或数据删除不完整
- 【Java】集合(List、Set、Map)遍历、删除、比较元素时的小陷阱
- Java 集合(List、Set)遍历、判断、删除元素时的小陷阱
- 遍历容器时出现的java.util.ConcurrentModificationException
- 外部系统调用SAP接口如何调试
- Some projects cannot be imported because they already exist in the workspace
- 简单记录jsp
- hdu4635(强连通)
- 51nod1068 博弈
- java 遍历list、set时 删除元素 java.util.ConcurrentModificationException
- Android通过反射获取build.prop中key对应的value
- 提升方法 - 1 - Adaboost 算法
- 设计模式3—行为型模式
- C# 提示语句总汇
- ssh 导出excel
- AJAX-JQUERY
- 前端html/css布局注意点
- 含有蓝牙功能的App上线AppStore被拒的原因