简体   繁体   中英

How to remove List iteration without ConcurrentModificationException

My allWordsList is something like: [aaa, bbb, ccc, ddd, eee]

How make a copy ( tempWordsList ) of allWordsList in

for (String aWord : aWordsList) 

without the iteration item ( ie to achieve [bbb, ccc, ddd, eee] , then [aaa, ccc, ddd, eee] , etc...)?

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. Any change that you make to tempWordsList happens to aWordsList at the same exact time:

tempWordsList = aWordsList;

Therefore, myList will have multiple copies of the last modification of aWordList :

myList.add(tempWordsList);

adds the same object to myList at each iteration of the loop.

In order to make aWordsList and tempWordsList you need to replace the assignment with a copy, like this:

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...
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM