首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向量上的ConcurrentModificationError

向量上的ConcurrentModificationError
EN

Stack Overflow用户
提问于 2011-04-30 08:45:54
回答 1查看 98关注 0票数 0

我在这里试图完成的是,每当检测到碰撞时,从Vector中移除一个“花朵”。然而,我一直得到一个ConcurrentModificationError。当我试图从矢量中移除花朵时,它会变得一团糟。我尝试过很多方法。当检测到花朵应该被移除时,我将它的位置保存在Vector中,然后在列表中的下一个位置被查看时尝试将其移除。我认为这是您需要查看的唯一方法。有人知道我能做些什么来解决这个问题吗??

代码语言:javascript
复制
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);

                }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-30 09:02:33

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

代码语言:javascript
复制
synchronize(blossomVector)  
{  
    Iterator dataIterator = blossomVector.iterator();  
    while (dataIterator.hasNext())  
    {  
        //... do your stuff here and use dataIterator.remove()

    }  
}  
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5838697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档