简体   繁体   中英

I cannot figure out why I have this error: ConcurrentModificationException

I'm writing a program that's made to solve the Mastermind game. The gist of the program is to take a list of all the possible solutions and after every guess that isn't the correct one, remove anything from the list that wouldn't at least give that solution. This method is made to compare two Strings (guess and strFromArray) to see if they get the same values. However, I'm getting an error and I can't figure out why. Any help would be appreciated.

    Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at Game.shortenTheList(Game.java:88)
at Game.guess(Game.java:76)
at Game.play(Game.java:40)
at Game.main(Game.java:23)


/*
 * Compares the two strings. If they would get the same output, return false. If they would get a different output, return true.
 */
public boolean compare(String guess, String strFromArray, int black, int white)
{
    int white2 = 0;
    int black2 = 0;

    char[] arr1 = guess.toCharArray();
    char[] arr2 = strFromArray.toCharArray();

    for(int i=0; i<guess.length(); i++)
    {
        if(arr1[i] == arr2[i])
        {
            black2 ++;
            arr1[i] = '$';
            arr2[i] = '%';
        }
    }

    for(int i=0; i<guess.length(); i++)
    {
        for(int j=0; j<strFromArray.length(); j++)
        {
            if(arr1[i] == arr2[j])
            {
                white2++;
                arr1[i] = '!';
                arr2[j] = '@';
            }
        }
    }

    if(black == black2 && white == white2)
        return false;

    else
        return true;
}

/*
 * Shortens the list of possible solutions by eliminating everything that wouldn't get at least the given output.
 */
public void shortenTheList(String guess, int black1, int white1)
{
    for (String str : possibleSolutions)
    {
        if(compare(guess, str, black1, white1))
        {
            possibleSolutions.remove(str);
        }
    }
}

Once you open an Iterator (which you do implicitly when using for(String str: possibleSolutions) , any modifications to the underlying collection ( possibleSolutions ) except by calling remove on the Iterator will cause a ConcurrentModificationException ; this is very clearly documented on the collection classes.

If you need to remove items from the collection, use an explicit Iterator :

Iterator<String> it = possibleSolutions.iterator();
while(it.hasNext()) {
    if(compare(guess, it.next(), black1, white1))
        it.remove();
}

As @allprog pointed out, a functional approach is better when you have such a clear "filtering" problem. Until Java 8 is an option, using Guava's Iterables#filter or Iterables#removeIf is probably a good choice; you would just wrap your compare method and pass it in.

/*
 * Shortens the list of possible solutions by eliminating everything that wouldn't get at least the given output.
 */
public void shortenTheList(String guess, int black1, int white1)
{
    for (String str : possibleSolutions)
    {
        if(compare(guess, str, black1, white1))
        {
            possibleSolutions.remove(str);
        }
    }
}

There's your problem.

Instead, use:

/*
 * Shortens the list of possible solutions by eliminating everything that wouldn't get at least the given output.
 */
public void shortenTheList(String guess, int black1, int white1)
{
    Iterator<String> it = possibleSolutions.iterator();
    while(it.hasNext())
    {
        String str = it.next();
        if(compare(guess, str, black1, white1))
        {
            it.remove();
        }
    }
}

This is the only clean way to remove an object from a collection you're currently iterating over. The other, less elegant way would be to create a separate list of String s to remove and iterate over that.

You cannot modify the list possibleSolutions while you are iterating through it using a foreach loop.

Change your code to something like this:

public void shortenTheList(String guess, int black1, int white1)
{    
    for(Iterator<String> it = possibleSolutions.iterator(); it.hasNext()){
        String str = it.next();
        if(compare(guess, str, black1, white1)){
            it.remove();
        }
    }
}

Like chrylis says, you can't remove an element from a collection without using an Iterator.

example:

public void shortenTheList(String guess, int black1, int white1)
{
for (String str : possibleSolutions)
{
if(compare(guess, str, black1, white1))
{
possibleSolutions.remove(str);
}
}
}

should be:

public void shortenTheList(String guess, int black1, int white1)
{
Iterator i = possibleSolutions.Iterator();
while(i.hasNext())
{
String str = (String) i.next();
if(compare(guess, str, black1, white1))
{
i.remove();
}
}
}

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