I’m trying to copy ‘select’ elements from hands[1] to hands[0].
I can successfully do this with this code:
for(Card card : hands[1].cards) {
if (card.suit().ordinal() == 0){
hands[0].addSingleCard(card);
//hands[1].removeSingleCard(card);
}
}
Unfortunately, my removeSingleCard method doesn’t work how i expected.
With it commented out, the for-each loop successfully copies all ‘Club’ cards from hands[1] to hands[0]. I was hoping the removeSingleCard method would delete each ‘Club’ card from hands[1] after it was copied.
public void addSingleCard(Card card){
if(card!= null){
cards.add(card);
}
}
public void removeSingleCard(Card c){
if(c!= null){
cards.remove(c);
}
}
Any ideas why this isn’t working?
You can’t remove from a collection you’re iterating over, other than via the iterator. So you could use: