从一个ArrayList中删除给我java.util.ConcurrentModificationException [英] Removing from an ArrayList gives me java.util.ConcurrentModificationException

查看:152
本文介绍了从一个ArrayList中删除给我java.util.ConcurrentModificationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此的Andr​​oid游戏,我用一个ArrayList来跟踪所有实体目前在屏幕上。通过列表跑步让我成功地更新,并添加实体画面/列表,但试图删除它们给了我一个java.util.ConcurrentModificationException错误。我是一个新手程序员,所以我真的不知道有什么可以去错了。

In this android game, I'm using an ArrayList to keep track of all of the entities currently on the screen. Running through the list allows me to successfully update and add entities to the screen / list, but attempting to remove them gives me a java.util.ConcurrentModificationException error. I'm a novice programmer, so I don't really know what could be going wrong.

我使用的是确定何时需要实体的方法被删除(当它屏幕外,但只能通过顶部)是这样的。

The method I'm using to determine when a entity needs to be removed ( when it goes offscreen, but only through the top ) is this.

private void deleteEntities() {
    for(Entity ent : entsOnLevel) {
        if((ent.getY() + ent.getImage().getHeight()) < 0) {
            this.entsOnLevel.remove(ent);
        }
    }
}

有人能向我解释什么可以去错了?

Could someone explain to me what could be going wrong?

推荐答案

您创建了一个迭代器和要删除他的当前项目,因此他无法找到下一个项目。

You created an iterator and you are removing his current item, hence he cant find the next item.

在这里阅读更多 - > <一个href=\"http://stackoverflow.com/questions/15444391/concurrentmodificationexception\">ConcurrentModificationException?

Read more here -> ConcurrentModificationException?

这篇关于从一个ArrayList中删除给我java.util.ConcurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆