java - 我不明白为什么会出现此错误 : ConcurrentModificationException

标签 java exception concurrency

我正在编写一个程序来解决 Mastermind 游戏。该程序的要点是列出所有可能的解决方案,并在每次猜测不正确后,从列表中删除至少不会给出该解决方案的任何内容。此方法用于比较两个字符串(guess 和 strFromArray)以查看它们是否获得相同的值。但是,我遇到了错误,但我不明白为什么。任何帮助,将不胜感激。

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

最佳答案

一旦打开迭代器(在使用for(String str: possibleSolutions)时隐式执行此操作,对底层集合的任何修改(possibleSolutions code>),除非在 Iterator 上调用 remove 将导致 ConcurrentModificationException;这在集合类中记录得非常清楚。

如果需要从集合中删除项目,请使用显式迭代器:

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

正如 @allprog 指出的那样,当您遇到如此明显的“过滤”问题时,函数式方法会更好。直到 Java 8 成为一个选项,使用 Guava 的 Iterables#filterIterables#removeIf可能是一个不错的选择;您只需包装您的 compare 方法并将其传入。

关于java - 我不明白为什么会出现此错误 : ConcurrentModificationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18727982/

相关文章:

java - 如何在 Java 中为每个对象获取唯一 ID?

Java lambda 不适用于完整定义

原始数组写入的 Java 并发可见性

java并发: multi-producer one-consumer

java - 使用 ConcurrentMap 双重检查锁定

java - SendRedirect 不适用于 JSP Scriptlet

java - 如何在JSP代码中获取所请求站点的IP地址?

java - 使用 FlowLayout 将 JPanel 添加到 JFrame

java - 如何修复 IllegalStateException?

java - 从另一个正在运行的 Java 应用程序捕获异常