ConcurrentModificationException在ArrayList中使用indexOf()时发生。

huangapple go评论74阅读模式
英文:

ConcurrentModificationException when using indexOf() in an ArrayList

问题

我在仅对ArrayList进行迭代并在循环中使用indexOf()进行应用程序逻辑时,遇到了ConcurrentModificationException异常。有人能解释一下背后的原因吗?根据我所了解,如果我在迭代时尝试向列表中添加/删除项目,就会出现这种情况。我是否漏掉了一些内容?

for (SomeClass c : listOfSomeClass)) {
    listOfSomeClass.indexOf(c);
}
英文:

I'm getting ConcurrentModificationException when I'am just iterating through an ArrayList and using indexOf() in the loop for my application logic. Can someone explain the reason behind this? From what I read, this should happen if I try to add/remove an item to/from the list while I'm still iterating. Am I missing some thing here?

for (SomeClass c : listOfSomeClass)) {
            listOfSomeClass.indexOf(c);
        }

答案1

得分: 0

你遇到了并发问题,那么,不是使用以下方式:

ArrayList<SomeClass> listOfSomeClass = new ArrayList<>();

尝试使用以下方式:

CopyOnWriteArrayList<SomeClass> contas = new CopyOnWriteArrayList<>();

CopyOnWriteArrayListjava.util.ArrayList 的线程安全变体,在这种变体中,所有变更操作(如添加、设置等)通过创建底层数组的新副本来实现。

这通常会带来很高的开销,但在遍历操作远远超过变更操作的情况下,可能比其他方法更有效。当您无法或不想在遍历操作上进行同步,但又需要防止并发线程之间的干扰时,这将非常有用。

来源:https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CopyOnWriteArrayList.html

还有其他解决您问题的选项,但我们需要查看您的所有代码才能提出更好的建议。

英文:

You are having a concurrency issues, then, instead of you do:

ArrayList&lt;SomeClass&gt; listOfSomeClass = new ArrayList&lt;&gt;();

Try to do:

CopyOnWriteArrayList&lt;SomeClass&gt; contas = new CopyOnWriteArrayList&lt;&gt;();

'CopyOnWriteArrayList' is a thread-safe variant of java.util.ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.

This is ordinarily too costly, but may be more efficient than alternatives when traversal operations vastly outnumber mutations, and is useful when you cannot or don't want to synchronize traversals, yet need to preclude interference among concurrent threads.

font: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CopyOnWriteArrayList.html

There are another options to solve your problem, but we need to see all your code to suggest a better help.

答案2

得分: -1

如果您想在遍历列表时删除或添加新项,您应该使用ListIterator。以下是代码示例:

package com.test.dal;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class LTest {

    public static void main(String[] args) {
        List<String> listOfSomeClass = new ArrayList<>();
        listOfSomeClass.add("1");
        listOfSomeClass.add("2");
        listOfSomeClass.add("3");
        listOfSomeClass.add("4");
        ListIterator<String> iterator = listOfSomeClass.listIterator();

        while (iterator.hasNext()) {
            String item = iterator.next();
            if (item.equals("2"))
                iterator.remove();
            else if (item.equals("4"))
                iterator.add("5");
        }
        System.out.println(listOfSomeClass);
    }
}
英文:

If you want to delete or add new item when you traverse list,you should use the ListIterator.This the code

package com.test.dal;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class LTest {


    public static void main(String[] args) {
        List&lt;String&gt; listOfSomeClass = new ArrayList&lt;&gt;();
        listOfSomeClass.add(&quot;1&quot;);
        listOfSomeClass.add(&quot;2&quot;);
        listOfSomeClass.add(&quot;3&quot;);
        listOfSomeClass.add(&quot;4&quot;);
        ListIterator&lt;String&gt; iterator = listOfSomeClass.listIterator();

        while (iterator.hasNext()) {
            String item = iterator.next();
            if (item.equals(&quot;2&quot;))
                iterator.remove();
            else if (item.equals(&quot;4&quot;))
                iterator.add(&quot;5&quot;);
        }
        System.out.println(listOfSomeClass);
    }
}

huangapple
  • 本文由 发表于 2020年9月9日 05:43:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63801958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定