Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“线程中的异常”;“主要”;java.util.ConcurrentModificationException(第一本java书籍示例)_Java_Android_Java.util.concurrent_Concurrentmodification - Fatal编程技术网

“线程中的异常”;“主要”;java.util.ConcurrentModificationException(第一本java书籍示例)

“线程中的异常”;“主要”;java.util.ConcurrentModificationException(第一本java书籍示例),java,android,java.util.concurrent,concurrentmodification,Java,Android,Java.util.concurrent,Concurrentmodification,我试着运行HeadFirstJavaBook-SecondEdition(第152页)中的一个示例。这是一个益智游戏。它以网格中的点com(字符串)为目标,以使其沉没。但是当我试图运行这个谜题时,我得到了一个Java.util.concurrentModificationException错误。我只有在尝试将最后一个网络(字符串)放入网格时才出现此错误。假设网格中有三个字符串,如下图所示。因此,当我开始提供输入时,我可以删除(命中)前两个字符串(在arrayList中添加),但当我尝试删除最后一

我试着运行HeadFirstJavaBook-SecondEdition(第152页)中的一个示例。这是一个益智游戏。它以网格中的点com(字符串)为目标,以使其沉没。但是当我试图运行这个谜题时,我得到了一个Java.util.concurrentModificationException错误。我只有在尝试将最后一个网络(字符串)放入网格时才出现此错误。假设网格中有三个字符串,如下图所示。因此,当我开始提供输入时,我可以删除(命中)前两个字符串(在arrayList中添加),但当我尝试删除最后一个字符串(在arrayList中添加到最后一个的字符串)时,会出现concurrentmodificationException错误。下面给出了示例代码和控制台日志。
那么我为什么会出现此错误以及如何删除此错误。

class DotComBust{

    private GameHelper helper = new GameHelper();
    private ArrayList<DotCom> dotComList = new ArrayList<DotCom>();
    private int numofguesses=0;

    private void setUpGame(){
        DotCom one = new DotCom();
        one.setNames("Pets.com");
        DotCom two = new DotCom();
        two.setNames("eToys.com");
        DotCom three = new DotCom();
        three.setNames("Go2.com");
        dotComList.add(one);
        dotComList.add(two);
        dotComList.add(three);

        System.out.println("your goal is sunk three dot coms");
        System.out.println("Pets.com, eToys.com, Go2.com");
        System.out.println("Try to sink them all in fewest number of guesses");

        for(DotCom dotComToSet : dotComList){
            ArrayList<String> newLocation = helper.placeDotCom(3);
            dotComToSet.setLocationCells(newLocation);
        }

    }

        private void startPlaying(){
            while(!dotComList.isEmpty()){
                String userGuess = helper.getUserInput("Enter a guess");
                checkUserGuess(userGuess);
            }

            finishGame();
        }

        private void checkUserGuess(String userGuess){
            numofguesses++;
            String result = "miss";
            for(DotCom dotComToTest : dotComList){
                result = dotComToTest.checkYourself(userGuess);

                if(result.equals("hit")){
                    break;
                }
                if(result.equals("kill")){
                    dotComList.remove(dotComToTest);
                }
            }
        System.out.println(result);
        }


        private void finishGame(){
            System.out.println("All the dotcoms are dead your stock is worthless");
            if(numofguesses<=18){
                System.out.println("It only took you" + numofguesses + "guesses");
                System.out.println("You got out before your option sank");

            }else{
                System.out.println("Took you long enough" + numofguesses + "guesses");
                System.out.println("Fish are dancing with your option");
            }
        }

        public static void main(String[] args) {
            DotComBust game = new DotComBust();
            game.setUpGame();
            game.startPlaying();
        }

}


因为当你循环收集的时候

 while(!dotComList.isEmpty()){
            String userGuess = helper.getUserInput("Enter a guess");
            checkUserGuess(userGuess);
 }
您不允许修改列表的内容,但在您的方法checkUserGuess中,您试图从列表中删除元素

if(result.equals("kill")){
                    dotComList.remove(dotComToTest);
}

这就是导致ConcurrentModificationException的原因

我想您可能同时运行另一个绘图线程

在这种情况下,使用名为
CopyOnWriteArrayList
列表的线程安全实现:


private ArrayList dotComList=new CopyOnWriteArrayList()

好的,为什么不应用此选项?为什么我以非串行方式(如B1、D1、C1)输入,只有在尝试以串行方式(如B1、B2、B3)输入时才会出现错误。为什么会发生这种情况?当你解释原因时,我的回答已经解释了解决方案以及如何处理问题。没有理解的否决票显然是不成熟的。@Android开发者,可能只有当你输入序列值时,你的结果才等于杀死。调试并找出答案。
if(result.equals("kill")){
                    dotComList.remove(dotComToTest);
}