Removing while iterating issues in java
<p>I want to implement a method attack that works like this: every warrior from my troop attacks an warrior chosen randomly from another troop. If the attacked warrior dies it must be removed from the troop. With the method that i tried i get the error for the random number:</p>

<pre><code> java.lang.IllegalArgumentException: n must be positive
</code></pre>

<p>The troop is a List <code>&lt;Creature&gt;</code> warriors; I think that i am not doing the remove correctly, because otherwise i should not have that error.</p>

<pre><code>public void atac(Troop opponentTroop){
        for(Creature f : warriors){
            Creature c = getOpponent(opponentTroop);
            f.atac(c);
            ListIterator&lt;Creature&gt; iterator = opponentTroop.warriors.listIterator();
            while(iterator.hasNext()){
                c = iterator.next();
                if(c.isDead()){
                    iterator.remove();                  
                }
            }   

        }       
    }


private Creature getOpponent(Troop opponent){
        int x = rand.getRandomArrayIndex(opponent.warriors.size());
        return opponent.warriors.get(x);
}
</code></pre>
