相关疑难解决方法(0)

并发修改例外

我有这段小代码,它给了我并发修改异常.我无法理解为什么我一直得到它,即使我没有看到任何同时进行的修改.

import java.util.*;

public class SomeClass {
    public static void main(String[] args) {
        List<String> s = new ArrayList<>();
        ListIterator<String> it = s.listIterator();

        for (String a : args)
            s.add(a);

        if (it.hasNext())
            String item = it.next();

        System.out.println(s);
    }
}
Run Code Online (Sandbox Code Playgroud)

java concurrentmodification

43
推荐指数
5
解决办法
7万
查看次数

在ArrayList中添加foreach循环时,ConcurrentModificationException

我正在尝试使用arraylist的foreach循环,但是当我使用它时,它会给我错误,但是当我使用普通for循环时,它完美地工作,可能是什么问题?

代码在这里:

for (Pair p2 : R) {
    if ((p2.getFirstElm() == p.getSecondElm()) && (p2.getFirstElm() != p2.getSecondElm())) 
        R.add(new Pair (p.getFirstElm(), p2.getSecondElm()));
    else if ((p2.getSecondElm() == p.getFirstElm()) && (p2.getFirstElm() != p2.getSecondElm())) 
        R.add(new Pair (p2.getFirstElm(), p.getSecondElm()));

    // else
    // There are no transitive pairs in R.
}
Run Code Online (Sandbox Code Playgroud)

这是不起作用的循环,这是一个有效的循环:

for (int i = 0; i < R.size(); i++) {
    if ((R.get(i).getFirstElm() == p.getSecondElm()) && (R.get(i).getFirstElm() != R.get(i).getSecondElm())) 
        R.add(new Pair (p.getFirstElm(), R.get(i).getSecondElm()));
    else if ((R.get(i).getSecondElm() == p.getFirstElm()) && (R.get(i).getFirstElm() != R.get(i).getSecondElm())) 
        R.add(new Pair (R.get(i).getFirstElm(), p.getSecondElm())); …
Run Code Online (Sandbox Code Playgroud)

java foreach for-loop arraylist

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

为什么列表的反向子列表的List.addAll导致ConcurrentModificationException

我一直在尝试获取列表子列表,将其反转,然后将反转的列表放回起始位置。例如,假设我们拥有列表[1, 2, 3, 4, 5, 6],然后从索引2反转到索引4将得到[1, 2, 5, 4, 3, 6]

我为此编写了一些代码,但是ConcurrentModificationException每次都给出一个代码(除非startIndex == endIndex)。下面提供了一个最小的可重现示例:

int startIndex = 2;
int endIndex = 4;
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);

List<Integer> toReverse = list.subList(startIndex, endIndex+1);
Collections.reverse(toReverse);
list.removeAll(toReverse);
list.addAll(startIndex, toReverse);
Run Code Online (Sandbox Code Playgroud)

异常线程“main” java.util.ConcurrentModificationException
在java.util.ArrayList中的$ SubList.checkForComodification(来源不明)
在java.util.ArrayList的$ SubList.size(来源不明)在
java.util.AbstractCollection.toArray(来源不明)在 test.ConcurrentExample.main(ConcurrentExample.java:64)处
java.util.ArrayList.addAll(Unknown Source

错误所指的实际行是list.addAll(startIndex, toReverse);

我不确定是什么问题,因为迭代过程中似乎没有任何变化。如果有人能解释为什么会这样和/或如何解决它,将不胜感激。

java collections arraylist concurrentmodification

16
推荐指数
2
解决办法
145
查看次数

与LinkedHashMap的ConcurrentModificationException

java.util.ConcurrentModificationException当我LinkedHashMap在下面的代码中迭代结构时,不确定触发的是什么.使用这种Map.Entry方法很好.没有从之前的帖子中得到关于触发这个问题的一个很好的解释.

任何帮助,将不胜感激.

import java.util.LinkedHashMap;
import java.util.Map;

public class LRU {

    // private Map<String,Integer> m = new HashMap<String,Integer>();
    // private SortedMap<String,Integer> lru_cache = Collections.synchronizedSortedMap(new TreeMap<String, Integer>());

    private static final int MAX_SIZE = 3;

    private LinkedHashMap<String,Integer> lru_cache = new LinkedHashMap<String,Integer>(MAX_SIZE, 0.1F, true){
        @Override
        protected boolean removeEldestEntry(Map.Entry eldest) {
            return(lru_cache.size() > MAX_SIZE);
         }
    };    

    public Integer get1(String s){
        return lru_cache.get(s);        
    }

    public void displayMap(){
        /**
         * Exception in thread "main" java.util.ConcurrentModificationException
            at java.util.LinkedHashMap$LinkedHashIterator.nextEntry(LinkedHashMap.java:373)
            at java.util.LinkedHashMap$KeyIterator.next(LinkedHashMap.java:384)
            at LRU.displayMap(LRU.java:23)
            at …
Run Code Online (Sandbox Code Playgroud)

java linkedhashmap concurrentmodification

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

删除Java ArrayList中的重复对象会导致并发修改错误

可能重复:
并发修改异常:添加到ArrayList

我正在尝试使用此方法从arraylist中删除重复值:

 public void hasDuplicates(List<Artifact> p_cars) {
    final List<String> usedNames = new ArrayList<String>();
    for (Artifact car : p_cars) {
        final String name = car.getObjectId();

        if (usedNames.contains(name)) {
            p_cars.remove(car);

        }

        usedNames.add(name);
    }

}
Run Code Online (Sandbox Code Playgroud)

如何在不同时修改arraylist的情况下删除这些重复值?

我在一个arraylist上这样做,如果这有助于上下文,则填充listview.

谢谢!

java android listview arraylist

2
推荐指数
1
解决办法
3271
查看次数