java.util.ConcurrentModificationException 异常错误

标签 java exception arraylist

我正在尝试使用 GUI 制作一个 java 应用程序。 我正在编写一个代码,我想让用户更改一些数据并将这些更改保存在文本文件中。在此之前,我想删除从列表中更改的旧数据,然后用最后的更改重写新数据。如果我缺少您想查看的任何类(class),请告诉我,我会尽快将其放在网上

这是我的

    public void saveChanges(footBall Player, String name, String level,
        int[] scores, int footSize) {

    try {
        if (CompetitorsList.size() != 0) {
            for (Competitors C : CompetitorsList) {

                if (C instanceof footBall) {
                    String Number = Player.playerNumber + "";
                    if (C.getPlayerNumberAsString().equals(Number)) {
                        System.out.println("c");

 //the error hit me here when i try to remove the object from the list the exception error is       java.util.ConcurrentModificationException 
                        CompetitorsList.remove(C);

                    }

                }
            }

            Name NewName = new Name(name);
            System.out.println("Please get in2");
            footBall NewPlayer = new footBall(Player.playerNumber, scores,
                    level, footSize, NewName);

            CompetitorsList.add(NewPlayer);

            SaveOnFile();
        } else {
            System.out.println("No List");
        }

    } catch (Exception ex) {
        System.out.print("testing4");
        System.out.print("something wrong" + ex);
    }

}

这是 SaveOnFile 方法: 公共(public)无效SaveOnFile(){

    String scoresInString;
    FileWriter fw;
    try {

        fw = new FileWriter("footBall");

        for (Competitors C : CompetitorsList) {
            if (C instanceof footBall) {
                footBall Scores = new footBall();
                scoresInString = Scores.returnScoreAsString(C.scores);
                fw.write(C.playerNumber + ", " + C.name.getFullName()
                        + ", " + C.level + ", " + scoresInString + ","
                        + ((footBall) C).footSize() + "\n");
                fw.write("\r\n");
            }

        }

        fw.close();

    }
    // message and stop if file not found
    catch (FileNotFoundException fnf) {
        System.out.println("File not found ");
        System.exit(0);
    }
    // stack trace here because we don't expect to come here
    catch (IOException ioe) {
        ioe.printStackTrace();
        System.exit(1);
    }
}

最佳答案

对集合调用remove() 会使所有 Activity 迭代器失效。相反,您必须使用 Iterator.remove() 方法:

for(Iterator<Competitors> it = CompetitorsList.iterator(); it.hasNext(); ) {
    Competitors C = it.next();
    if(C instanceof ...) {
        if(C.getPlayerNumberAsString().equals(Number))
            it.remove();
    ...

这样,iterator() 就知道集合如何变化,否则这是不可能的,因为 ArrayList 不跟踪它生成的迭代器。

关于java.util.ConcurrentModificationException 异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8058505/

相关文章:

ruby - 从不同的地方重新提出 ruby 异常

java - 需要帮助遍历 ArrayList

java - 将 ArrayList 元素克隆到同一个 ArrayList

java - 从 String 创建 int[] 数组(仅限正整数)

java - 为我想要在java中的每个类创建一个类形式gui

java - 如何在java中从特定行读取文件直到文件末尾

java - 将 XML 文件编码为 Java pojo/域对象

java - 在方法签名中显式显示 "throw exception"的效果是什么

jquery - 未输入时的按键事件

java - 扫描仪没有停止