我在这里试图完成的是,每当检测到碰撞时,从Vector中移除一个“花朵”。然而,我一直得到一个ConcurrentModificationError。当我试图从矢量中移除花朵时,它会变得一团糟。我尝试过很多方法。当检测到花朵应该被移除时,我将它的位置保存在Vector中,然后在列表中的下一个位置被查看时尝试将其移除。我认为这是您需要查看的唯一方法。有人知道我能做些什么来解决这个问题吗??
private synchronized void DrawBlossoms(Canvas c) // method to draw flowers on screen and test for collision
{
Canvas canvas = c;
for(Blossom blossom: blossomVector)
{
blossom.Draw(canvas);
if (blossom.hit(box_x,box_y, box_x + boxWidth, box_y + boxHeight, blossomVector) == true)
{
Log.v(TAG, "REMOVE THIS!");
//blossomVector.remove(blossom);
}
}
}发布于 2011-04-30 09:02:33
解决方案是使用迭代器并在Vector上进行同步。
synchronize(blossomVector)
{
Iterator dataIterator = blossomVector.iterator();
while (dataIterator.hasNext())
{
//... do your stuff here and use dataIterator.remove()
}
} https://stackoverflow.com/questions/5838697
复制相似问题