Java 编程错误:java.util.ConcurrentModificationException

发布于 2024-12-21 22:20:36 字数 1074 浏览 1 评论 0原文

我正在编写一个程序作为 Java 初学者教程的一部分。我有以下方法,每当我运行它时,它都会给出以下异常:

  java.util.ConcurrentModificationException
        at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
        at java.util.AbstractList$Itr.next(AbstractList.java:343)
        at Warehouse.receive(Warehouse.java:48)
        at MainClass.main(MainClass.java:13)

这是方法本身,在 Warehouse 类中:

public void receive(MusicMedia product, int quantity) {

  if ( myCatalog.size() != 0) { // Checks if the catalog is empty

    // if the catalog is NOT empty, it will run through looking to find
    // similar products and add the new product if there are none
    for (MusicMedia m : myCatalog) { 
        if ( !m.getSKU().equals(product.getSKU()) ) {
                myCatalog.add(product);
        }
    }

  } else { // if the catalog is empty, just add the product
        myCatalog.add(product);
  }
}

问题似乎出在 if else 语句上。如果我不包含 if else,则程序将运行,尽管它无法正常工作,因为循环不会迭代空的 ArrayList。

我尝试添加一个产品只是为了防止它在代码的其他部分为空,但它仍然给我同样的错误。有什么想法吗?

I'm writing a program as part of tutorial for a beginner Java student. I have the following method and whenever I run it, it gives me the following exception:

  java.util.ConcurrentModificationException
        at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
        at java.util.AbstractList$Itr.next(AbstractList.java:343)
        at Warehouse.receive(Warehouse.java:48)
        at MainClass.main(MainClass.java:13)

Here's the method itself, within the class Warehouse:

public void receive(MusicMedia product, int quantity) {

  if ( myCatalog.size() != 0) { // Checks if the catalog is empty

    // if the catalog is NOT empty, it will run through looking to find
    // similar products and add the new product if there are none
    for (MusicMedia m : myCatalog) { 
        if ( !m.getSKU().equals(product.getSKU()) ) {
                myCatalog.add(product);
        }
    }

  } else { // if the catalog is empty, just add the product
        myCatalog.add(product);
  }
}

The problem seems to be with the if else statement. If I don't include the if else, then the program will run, although it won't work properly because the loop won't iterate through an empty ArrayList.

I've tried adding a product just to keep it from being empty in other parts of the code, but it still gives me the same error. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎言笑 2024-12-28 22:20:36

您无法迭代要添加内容的同一个列表。单独列出要添加的内容,然后将它们全部添加到最后。

You can't be iterating through the same list you're going to add things to. Keep a separate list of the things you're going to add, then add them all at the end.

在迭代时不得修改 mCatalog。您在此循环中向其中添加一个元素:

for (MusicMedia m : myCatalog) { 
    if ( !m.getSKU().equals(product.getSKU()) ) {
            myCatalog.add(product);
    }
}

请参阅 ConcurrentModificationExceptionmodCount摘要列表

You must not modify mCatalog while you're iterating over it. You're adding an element to it in this loop:

for (MusicMedia m : myCatalog) { 
    if ( !m.getSKU().equals(product.getSKU()) ) {
            myCatalog.add(product);
    }
}

See ConcurrentModificationException and modCount in AbstractList.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文