标签: concurrentmodification

Java中是否有一个可接受的最佳实践,用于在迭代列表时删除列表元素?

我发现有关避免一段ConcurrentModificationException时间这样做的最佳方法的相互矛盾的建议:

    List<Apple> Apples = appleCart.getApples();
    for (Apple apple : Apples)
    {
        delete(apple);
    }
Run Code Online (Sandbox Code Playgroud)

我倾向于使用Iterator代替a List并调用它的remove方法.

这在这里最有意义吗?

java list concurrentmodification

3
推荐指数
1
解决办法
1333
查看次数

将迭代器上的当前对象移动到列表的末尾

我在使用Iterator(LinkedList.iterator())对象的Java上遇到了问题.在循环中,我需要将迭代器对象从某个位置移动到列表末尾.

例如:

final Iterator<Transition> it = this.transitions.iterator();
while(it.hasNext()) {
    final Transition object = it.next();

    if(object.id == 3){
        // Move to end of this.transitions list
        // without throw ConcurrentModificationException
    }
}
Run Code Online (Sandbox Code Playgroud)

由于某些原因,我无法克隆this.transitions.有可能,或者我真的需要使用克隆方法?

编辑:目前,我这样做:

        it.remove();
        this.transitions.add(object);
Run Code Online (Sandbox Code Playgroud)

但问题只在于第二行.我无法添加itens,它是同一个对象的内部迭代器.:(

java iterator concurrentmodification

3
推荐指数
1
解决办法
1862
查看次数

Java List和递归导致并发修改异常

以下函数以递归方式遍历列表并将其除以一半,并对子列表执行某些操作.如果列表大小为2,则递归会中断.我知道如果在迭代它时更改列表,则会发生并发修改异常.但我不使用迭代,它仍然会发生:

    private static List<ParticipantSlot> divide(List<ParticipantSlot> list) {
        int n = list.size();

        //do something 

        if (n>2){
            List<ParticipantSlot> l = divide(list.subList(0, n/2-1));
            List<ParticipantSlot> r= divide(list.subList(n/2, n));

            l.addAll(r);
            return l;
        }else{
            return list;
        }
    }
Run Code Online (Sandbox Code Playgroud)

java recursion list concurrentmodification

3
推荐指数
1
解决办法
2593
查看次数

ConcurrentModificationException和HashSet.iterator()

我有一个for循环

      for (int neighbour : neighbours) {
Run Code Online (Sandbox Code Playgroud)

我可以neighbours在循环内修改的地方.发现那是事业的原因ConcurrentModificationException.并阅读/sf/answers/573266921/

因此,如果您想要修改列表(或一般的任何集合),请 使用迭代器,因为它知道修改,因此将正确处理.

所以我尝试过:

neighboursItr = neighbours.iterator();
while (neighboursItr.hasNext()) {
  // try disconnecting vertices
  neighbour = neighboursItr.next();
Run Code Online (Sandbox Code Playgroud)

但这并没有解决问题.为什么?

java iterator concurrentmodification

3
推荐指数
1
解决办法
9088
查看次数

Java中的并发修改异常

我在执行此代码时收到ConcurrentModificationException.我无法弄清楚它为什么会发生?

private void verifyBookingIfAvailable(ArrayList<Integer> list, int id) {

        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
                int value = iterator.next();
                if (value == id) {
                    int index = list.indexOf(id);

                    if (index != -1) {
                        list.remove(index);
                    }
                }
        }
    }
Run Code Online (Sandbox Code Playgroud)

提前致谢.

java list arraylist concurrentmodification listiterator

3
推荐指数
1
解决办法
1万
查看次数

使用iterator和iterator.remove()时出现ConcurrentModificationException

    private int checkLevel(String bigWord, Collection<String> dict, MinMax minMax)
{
    /*value initialised to losing*/
    int value = 0; 
    if (minMax == MinMax.MIN) value = 1; 
    else value = -1; 


    boolean go = true;

    Iterator<String> iter = dict.iterator();

    while(iter.hasNext())
    {
        String str = iter.next(); 
        Collection<Integer> inds = naiveStringSearch(bigWord, str);

        if(inds.isEmpty())
        {
            iter.remove();
        }

        for (Integer i : inds)
        {
            MinMax passin = minMax.MIN;
            if (minMax == MinMax.MIN) passin = minMax.MAX;

            int value2 = checkLevel(removeWord(bigWord, str, i), dict, passin); 
            if (value2 == -1 …
Run Code Online (Sandbox Code Playgroud)

java collections iterator concurrentmodification

3
推荐指数
2
解决办法
6853
查看次数

列出ConcurrentModificationException

我有以下代码

public void saveProjects(List<Project> proj) throws DatabaseException {
    for (Project listItems: proj) { // error here

        insertProjects(listItems);
    }
}

private void insertProjects(Project prj) throws DatabaseException {
    commitObjects(prj);
}
Run Code Online (Sandbox Code Playgroud)

当我执行上述操作时,我收到以下异常 for (Project listItems: proj) {

java.util.AbstractList中的java.util.ConcurrentModificationException $ Itr.checkForComodification(AbstractList.java:449)at java.util.AbstractList $ Itr.next(AbstractList.java:420)

如何使用next或with iterator解决此问题?

编辑1

我在调用saveProjects的代码片段

projectList.add(proj);
  for (Project persist: projectList) {
       persist.setProjectId("K7890");
       persist.setName(fileName);

          myDAO.saveProjects(projectList);

     }
  projectList.clear();
Run Code Online (Sandbox Code Playgroud)

java java.util.concurrent concurrentmodification

3
推荐指数
1
解决办法
1731
查看次数

Collections.sort方法有时会在多线程环境中抛出ConcurrentModificationException.列表未在结构上进行修改

    package CollectionsTS;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.List;

    public class ArrayListTS {
        public static void main(String[] args) {
            HashSet<Integer> hset = new HashSet<Integer>();
            for (int i = 0; i <= 1000; i++) {
                hset.add(i);
            }

            MyRunnable mr = new MyRunnable();
            mr.addElements(hset);

            Thread t1 = new Thread(mr,"t1");
            Thread t2 = new Thread(mr,"t2");
            Thread t3 = new Thread(mr,"t3");

            t1.start(); t2.start(); t3.start();

        }
    }

    class MyRunnable implements Runnable {

        List<Integer> ilist = new ArrayList<Integer>();

        public void addElements(HashSet<Integer> hset) {
            ilist.addAll(hset);
        } …
Run Code Online (Sandbox Code Playgroud)

java sorting collections multithreading concurrentmodification

3
推荐指数
1
解决办法
4444
查看次数

排序的ConcurrentModification异常

我写了这个小程序来排序arrays.根据我的理解,它应该打印0,1,2.

但是,当我运行这个程序时,我得到了 ConcurrentModificationException

    public class Test {
    public static void main(String[] args) {
    List<Double> l1 = new ArrayList<Double>(Arrays.asList(2., 0., 1.));
    List<Double> l2 = l1.subList(0, 3);
    Collections.sort(l1);
    System.out.println(l2.get(0));
    }
}
Run Code Online (Sandbox Code Playgroud)

我真的不确定这个例外的根本原因.

有人可以帮我理解我犯错的地方吗?

注意:这个问题在JAVA 7上不存在,如果有人也说JAVA 8中的原因而不是JAVA 7那就太棒了

java concurrentmodification

3
推荐指数
1
解决办法
74
查看次数

使用Iterator.next()避免ConcurrentModificationException

在我的Android应用中,我在地图上绘制一些航路点时使用了此代码

Iterator<Waypoint> iterator = waypoints.iterator();
while (iterator.hasNext()) {
   Waypoint w = iterator.next();
}
Run Code Online (Sandbox Code Playgroud)

但是我收到这个错误

致命异常:java.util.ConcurrentModificationException java.util.ArrayList $ ArrayListIterator.next(ArrayList.java:573)

不是直接在循环中修改列表。

但是我可能会在另一个线程中修改列表,因为用户可以移动一些航路点。用户可以在触摸屏上移动航路点的同时绘制航路点。

我可以以某种方式避免该异常吗?

java android iterator concurrentmodification

3
推荐指数
1
解决办法
1243
查看次数