0

我在这里要完成的是在检测到碰撞时从向量中删除“花朵”。但是,我不断收到 ConcurrentModificationError。当我尝试从 Vector 中移除花朵时,它搞砸了。我已经尝试过很多方法。有一次,当检测到应该移除花朵时,我将其位置保存在 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);

                }
    }
}
4

1 回答 1

1

解决方案是使用迭代器并在 Vector 上进行同步。

synchronize(blossomVector)  
{  
    Iterator dataIterator = blossomVector.iterator();  
    while (dataIterator.hasNext())  
    {  
        //... do your stuff here and use dataIterator.remove()

    }  
}  
于 2011-04-30T01:02:33.000 回答