jjzjj

ConcurrentModificationException

全部标签

java - 如何在没有 ConcurrentModificationException 的情况下使用 for-each 循环进行迭代时修改集合?

这个问题在这里已经有了答案:IteratingthroughaCollection,avoidingConcurrentModificationExceptionwhenremovingobjectsinaloop(30个回答)关闭6年前.如果我在使用for-each循环迭代集合时修改集合,它会给出ConcurrentModificationException。有什么解决办法吗? 最佳答案 使用Iterator#remove.这是在迭代期间修改集合的唯一安全方法。如需更多信息,请参阅TheCollectionInterface教程。

kotlin - 如何避免 ConcurrentModificationException kotlin

我有一个帐户列表,当我长按时,我想从数组列表中删除该项目。我试图从警报对话框中删除它,但我得到了ConcurrentModificationException。这是崩溃的地方:listAccounts.forEachIndexed{index,account->if(idParamether==account.id){listAccounts.remove(account)}} 最佳答案 这是JVM的一个常见问题,如果你想在迭代时从集合中删除一个项目,你需要使用Iterators示例:valmyCollection=mutableL

java - 为什么会抛出 ConcurrentModificationException 以及如何调试它

我正在使用Collection(JPA间接使用的HashMap,它确实如此),但显然代码随机抛出ConcurrentModificationException。是什么原因造成的,我该如何解决这个问题?也许通过使用一些同步?这是完整的堆栈跟踪:Exceptioninthread"pool-1-thread-1"java.util.ConcurrentModificationExceptionatjava.util.HashMap$HashIterator.nextEntry(UnknownSource)atjava.util.HashMap$ValueIterator.next(Unkn

java - 如何在迭代时从 "ConcurrentModificationException"中删除元素时避免 `ArrayList`?

这个问题在这里已经有了答案:IteratingthroughaCollection,avoidingConcurrentModificationExceptionwhenremovingobjectsinaloop(30个回答)关闭7年前。我正在尝试从ArrayList中删除一些元素,同时像这样迭代它:for(Stringstr:myArrayList){if(someCondition){myArrayList.remove(str);}}当然,在迭代myArrayList的同时尝试从列表中删除项目时,我得到了ConcurrentModificationException。有没有一些

java - ArrayList 的 ConcurrentModificationException

这个问题在这里已经有了答案:IteratingthroughaCollection,avoidingConcurrentModificationExceptionwhenremovingobjectsinaloop(30个回答)关闭7年前。我有以下代码:privateStringtoString(ListaDrugStrengthList){StringBuilderstr=newStringBuilder();for(DrugStrengthaDrugStrength:aDrugStrengthList){if(!aDrugStrength.isValidDrugDescriptio

java - ArrayList 的 ConcurrentModificationException

这个问题在这里已经有了答案:IteratingthroughaCollection,avoidingConcurrentModificationExceptionwhenremovingobjectsinaloop(30个回答)关闭7年前。我有以下代码:privateStringtoString(ListaDrugStrengthList){StringBuilderstr=newStringBuilder();for(DrugStrengthaDrugStrength:aDrugStrengthList){if(!aDrugStrength.isValidDrugDescriptio

java - 迭代并从 ArrayList 中删除元素时如何避免 java.util.ConcurrentModificationException

我有一个要迭代的ArrayList。在迭代它时,我必须同时删除元素。显然这会抛出java.util.ConcurrentModificationException.处理此问题的最佳做法是什么?我应该先克隆列表吗?我删除的不是循环本身的元素,而是代码的另一部分。我的代码如下所示:publicclassTest(){privateArrayListabc=newArrayList();publicvoiddoStuff(){for(Aa:abc)a.doSomething();}publicvoidremoveA(Aa){abc.remove(a);}}a.doSomething可能会调用

java - 迭代并从 ArrayList 中删除元素时如何避免 java.util.ConcurrentModificationException

我有一个要迭代的ArrayList。在迭代它时,我必须同时删除元素。显然这会抛出java.util.ConcurrentModificationException.处理此问题的最佳做法是什么?我应该先克隆列表吗?我删除的不是循环本身的元素,而是代码的另一部分。我的代码如下所示:publicclassTest(){privateArrayListabc=newArrayList();publicvoiddoStuff(){for(Aa:abc)a.doSomething();}publicvoidremoveA(Aa){abc.remove(a);}}a.doSomething可能会调用

java - 为什么在此示例中我没有收到 java.util.ConcurrentModificationException?

注意:我知道Iterator#remove()方法。在下面的代码示例中,我不明白为什么main方法中的List.remove会抛出ConcurrentModificationException,但是不在remove方法中。publicclassRemoveListElementDemo{privatestaticfinalListintegerList;static{integerList=newArrayList();integerList.add(1);integerList.add(2);integerList.add(3);}publicstaticvoidremove(Int

java - 为什么在此示例中我没有收到 java.util.ConcurrentModificationException?

注意:我知道Iterator#remove()方法。在下面的代码示例中,我不明白为什么main方法中的List.remove会抛出ConcurrentModificationException,但是不在remove方法中。publicclassRemoveListElementDemo{privatestaticfinalListintegerList;static{integerList=newArrayList();integerList.add(1);integerList.add(2);integerList.add(3);}publicstaticvoidremove(Int