简体   繁体   English

如何在没有ConcurrentModificationException的情况下删除List迭代

[英]How to remove List iteration without ConcurrentModificationException

My allWordsList is something like: [aaa, bbb, ccc, ddd, eee] 我的allWordsList类似于: [aaa, bbb, ccc, ddd, eee]

How make a copy ( tempWordsList ) of allWordsList in 如何进行复印( tempWordsList的) allWordsList

for (String aWord : aWordsList) 

without the iteration item ( ie to achieve [bbb, ccc, ddd, eee] , then [aaa, ccc, ddd, eee] , etc...)? 没有迭代项(即实现[bbb, ccc, ddd, eee] ,然后[aaa, ccc, ddd, eee]等)?

public class Anagrams {

    List<String> allWordsList = new ArrayList<String>();
    List<List<String>> myList = new ArrayList<List<String>>();
    List<String> tempWordsList = new ArrayList<String>();

    public Anagrams(String allWords) {
        getWordsList(allWordsList, allWords); // getting List to copy here
        getAnagramWordsList(allWordsList);
    }

    private void getAnagramWordsList(List<String> aWordsList) {
        for (String aWord : aWordsList){
            //tempWordsList.clear();
            tempWordsList = aWordsList;
            for (Iterator<String> iterator = tempWordsList.iterator(); iterator.hasNext();) {
                String string = iterator.next();
                if (string == aWord) {
                    // Remove the current element from the iterator and the list.
                    iterator.remove();
                }
            }
            myList.add(tempWordsList);
            System.out.println(aWordsList);
            System.out.println(tempWordsList); //before error both lists are without first item...
        }
    }

}

I went through a couple of similar cases but still don't understand it well. 我经历了几个类似的案例,但仍然不太了解。

The biggest problem in your code is that tempWordsList and aWordsList refer to the same object. 您的代码中最大的问题是tempWordsListaWordsList引用同一对象。 Any change that you make to tempWordsList happens to aWordsList at the same exact time: 您对tempWordsList任何更改aWordsList在同一确切时间发生在aWordsList上:

tempWordsList = aWordsList;

Therefore, myList will have multiple copies of the last modification of aWordList : 因此, myList会有最后修改的多个副本aWordList

myList.add(tempWordsList);

adds the same object to myList at each iteration of the loop. 在循环的每次迭代中将相同的对象添加到myList中。

In order to make aWordsList and tempWordsList you need to replace the assignment with a copy, like this: 为了制作aWordsListtempWordsList您需要用一个副本替换赋值,如下所示:

tempWordsList = new List<String>(aWordsList);

It should be like this : 应该是这样的:

Iterator<String> it = tempWordsList.iterator();
while(it.hasNext()){
    String value = it.next();
   // System.out.println("List Value:"+value);
   if (value.equals(aWord)) {
      it.remove();
        }
    }

The method that will solve your problem is the following, but it is not a good solve. 解决问题的方法如下,但这并不是一个好的解决方法。

// Solve //解决

private void getAnagramWordsList(List<String> aWordsList) {

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

    for (String aWord : aWordsList){
        //tempWordsList.clear();
        tempWordsList = aWordsList;

        for (int i = tempWordsList.size()-1; i > 0; i--) {
            if (string.equals(tempWordsList.get(i)) {
                toRemove.add(i);
            }
        }
        for(int idx = 0; idx < toRemove.size(); idx++)
          tempWordsList.remove(idx);

        myList.add(tempWordsList);

        System.out.println(aWordsList);
        System.out.println(tempWordsList); //before error both lists are without first item...
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 迭代列表 (ConcurrentModificationException) - Iteration over a list (ConcurrentModificationException) 可以在没有迭代的情况下在 HashMap 中获取、放置和删除元素导致 ConcurrentModificationException? - Can get, put & remove element in HashMap without iteration cause ConcurrentModificationException? Java中的ConcurrentModificationException即使没有迭代 - ConcurrentModificationException in java even without iteration 如何在没有ConcurrentModificationException的情况下删除哈希图中的多个映射? - How to remove multiple mappings in hashmap without ConcurrentModificationException? 通过列表迭代,同步并使用迭代器时发生ConcurrentModificationException - ConcurrentModificationException on iteration through list, synchronized and using iterators 对已排序的列表进行排序时,列表迭代会在Java 8中引发ConcurrentModificationException - List iteration throws ConcurrentModificationException in Java 8 when sorting already sorted list Java在没有ConcurrentModificationException的情况下在同一循环中添加/删除 - Java add/remove at the same loop without ConcurrentModificationException 如何在没有ConcurrentModificationException的情况下修改subList? - How to modify subList without ConcurrentModificationException? 使用list.remove()时出现Java ConcurrentModificationException - Java ConcurrentModificationException when using list.remove() java.util.ConcurrentModificationException不列出删除错误 - java.util.ConcurrentModificationException Not list remove error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM