thread safety - ConcurrentModificationError on Vector -


what trying accomplish here remove "blossom" vector whenever collision detected. however, keep getting concurrentmodificationerror. messes when try remove blossom vector. have tried doing many ways. @ 1 point when detected blossom should removed, saved position in vector , tried remove when next position in list being looked at. think method need see. can see can fix this??

private synchronized void drawblossoms(canvas c) // method draw flowers on screen , test 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);                  }     } } 

the solution use iterator , synchronize on vector.

synchronize(blossomvector)   {       iterator dataiterator = blossomvector.iterator();       while (dataiterator.hasnext())       {           //... stuff here , use dataiterator.remove()      }   }   

Comments