我们都知道你不能这样做:
for (Object i : l) {
if (condition(i)) {
l.remove(i);
}
}
Run Code Online (Sandbox Code Playgroud)
ConcurrentModificationException等等......这显然有时起作用,但并非总是如此.这是一些特定的代码:
public static void main(String[] args) {
Collection<Integer> l = new ArrayList<>();
for (int i = 0; i < 10; ++i) {
l.add(4);
l.add(5);
l.add(6);
}
for (int i : l) {
if (i == 5) {
l.remove(i);
}
}
System.out.println(l);
}
Run Code Online (Sandbox Code Playgroud)
当然,这会导致:
Exception in thread "main" java.util.ConcurrentModificationException
Run Code Online (Sandbox Code Playgroud)
...即使多线程没有这样做......无论如何.
什么是这个问题的最佳解决方案?如何在循环中从集合中删除项而不抛出此异常?
我也在Collection这里使用任意,不一定是ArrayList,所以你不能依赖get.
我理解在Java中,Collection<E>不应该在迭代它时修改它,例如删除或添加元素.但是如何更改List中的元素呢?例如,如果有的话
List<String> letters = new ArrayList<String>();
letters.add("A");
letters.add("B");
letters.add("C");
int i = 0;
for (String letter : letters) {
letters.set(i, "D");
i++;
}
Run Code Online (Sandbox Code Playgroud)
所以,我不是在谈论修改存储在元素中的对象; 我在谈论改变对象是什么.List的大小没有被改变,但索引处的对象正在改变,所以从技术上讲,List正在被修改.我的老板声称这段代码很好(而且看起来确实有效),但我仍然认为它不正确.使用ListIterator的set(E e)方法的另一种方法是更好吗?
我正在尝试创建一个霍夫曼树,并且正在尝试合并两棵树.我无法弄清楚如何在没有得到"并发修改异常"的情况下删除程序中的树,因为我正在迭代列表并尝试同时从列表中删除.
BinaryTree<Character, Integer> t1 = null;
BinaryTree<Character, Integer> t2 = null;
BinaryTree<Character, Integer> tFinal = null;
int treeSize = TREES.size();
for (int i = 0; i < treeSize; i++) {
for (BinaryTree<Character, Integer> t : TREES) {
System.out.println("treeSize " + treeSize);
System.out.println(t.getRoot().getElement()
+ " t.getRoot().getElement()");
// here I edited the merge function in Binary Tree to set
// the new root
// to have null value for value, and itemTwo for weight
System.out.println(t.getRoot().getValue() + " weight of tree \n");
t1 …Run Code Online (Sandbox Code Playgroud) 我试图从列表中删除对象,我得到以下异常:
failure:java.util.ConcurrentModificationException null
Run Code Online (Sandbox Code Playgroud)
这就是我尝试从列表中删除对象的方法:
private List<testVO> removeDuplicateEntries(List<testVO> sessionList,List<testVO> dbList){
for (Iterator<testVO> dbIterator = dbList.listIterator(); dbIterator.hasNext(); ) {
testVO voDB = dbIterator.next();
for (Iterator<testVO> sessionIterator = sessionList.iterator(); sessionIterator.hasNext();) {
testVO voSession = (testVO) sessionIterator.next();
if(voDB.getQuestionID().intValue() == voSession.getQuestionID().intValue()){
//remove the object from sesion list
sessionIterator.remove();
//Add the object from DB to session list
sessionList.add(voDB);
}
}
}
return sessionList;
}
Run Code Online (Sandbox Code Playgroud)
我想删除当前的副本sessionList并添加来自的重复项dbList.
我执行了以下代码
Map<String, SyncPrimitive> syncPrimitives = new HashMap<String, SyncPrimitive>();
for (SyncPrimitive primitive : this.getSyncPrimitives()) {
String groupId = primitive.getId();
primitive.onConnect(groupId);
}
Run Code Online (Sandbox Code Playgroud)
然后我得到以下异常
Error while calling watcher
java.util.ConcurrentModificationException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java:793)
at java.util.HashMap$KeyIterator.next(HashMap.java:828)
Run Code Online (Sandbox Code Playgroud)
在onConnect方法中,原始对象被修改.我怎样才能克服这个问题?