4 Replies Latest reply on Jun 8, 2005 1:39 AM by dumbledore

    Deleting while iterating over a collection

    dumbledore

      Hi,

      using JBoss-4.0.2 with applied Patch (Principal-Propagation-Probems, @see Announcements), this doesn't work correctly, since it only deletes one of two players:

      
       java.util.Iterator iterator = game.getPlayers().iterator();
       while (iterator.hasNext()) {
       player = (PlayerLocal) iterator.next();
       logger.debug("removing player with namePK="+player.getNamePK());
       iterator.remove();
       }
      
      



      But this works:
       Object[] playerArray = game.getPlayers().toArray();
       for (int index=0; index<playerArray.length; index++) {
       player = (PlayerLocal) playerArray[index];
       logger.debug("removing player with namePK="+player.getNamePK());
       player.remove();
       }
      


      I think the code with the iterator should work, no?