mallorn
mallorn

Reputation: 815

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 ( i.e. 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.

Upvotes: 2

Views: 93

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727137

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);

Upvotes: 2

R. Suntjens
R. Suntjens

Reputation: 121

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

Upvotes: 1

Madushan Perera
Madushan Perera

Reputation: 2598

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();
        }
    }

Upvotes: 1

Related Questions