Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.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 ConcurrentModificationException_Java_Libgdx_Concurrentmodification - Fatal编程技术网

带有抽象超类的Java ConcurrentModificationException

带有抽象超类的Java ConcurrentModificationException,java,libgdx,concurrentmodification,Java,Libgdx,Concurrentmodification,所以我明白为什么我会出错;我想知道一个工作周围,如果可能或替代 因此,我有一个名为SpriteRenderable的类,用于对屏幕上的所有对象进行排序和渲染,它包含实例的位置和sprite public static ArrayList<SpriteRenderable> spriteRenderables; 我有另一个类Player,它扩展了SpriteRenderable类,因此它会自动添加到静态ArrayList中,并在每个渲染周期更新。这与枪和子弹类相同 Gun类和Bull

所以我明白为什么我会出错;我想知道一个工作周围,如果可能或替代

因此,我有一个名为
SpriteRenderable
的类,用于对屏幕上的所有对象进行排序和渲染,它包含实例的位置和sprite

public static ArrayList<SpriteRenderable> spriteRenderables;
我有另一个类
Player
,它扩展了
SpriteRenderable
类,因此它会自动添加到静态ArrayList中,并在每个渲染周期更新。这与
子弹
类相同

Gun
类和
Bullet
类也扩展了
SpriteRenderable
。在
Player
classes update函数中,如果
(Gdx.input.isButtonPressed(input.Buttons.LEFT))
则调用Guns shot方法,该方法在tern中调用
Bullet shot=new Bullet()

在玩家更新,玩家开火之前,这一切正常;实例化的项目符号在循环自身时添加到静态ArrayList。导致:

Exception in thread "LWJGL Application" java.util.ConcurrentModificationException

我已经尝试过用一个try-and-catch环绕foreach(SpriteRenderable)循环;但是,由于该异常,最终无法渲染任何精灵

假设您对列表的迭代次数多于对列表的修改次数,那么您最好使用
CopyOnWriteArrayList
。但是,迭代器生命周期内的任何修改对迭代器都是不可见的。

请提供一个示例。在对集合进行迭代时,您正在更改集合的状态,这在没有interator的情况下是不允许的,但是,您的整个程序结构可能会出现问题,由于你大量使用静力学,这很可能是一种伪装。我们需要更多的代码和信息,ArrayList不是线程安全的。当您在迭代时更新列表时,它会导致并发修改异常。您可以尝试CopyOnWriteArrayList,这是Java 5并发API中引入的一个并发集合类。尝试使用LibGDX的SnapshotArray,以便在循环时可以安全地编辑数组信息技术使用说明在其Javadoc中。
Exception in thread "LWJGL Application" java.util.ConcurrentModificationException