I have a List
of Integers
that contains 1's and 2's. While iterating thru the loop, I'd have 2 if
blocks and at the end I'd remove the first Integer
of the current List
.
I tried using an Iterator and do remove()
but it also removes all the occurrences of the Integer
I might want to keep even though I'd only like to remove the first one.
This is what my code would look like :
Collections.reverse(currentPlayer);
for (Iterator<Integer> iterator = currentPlayer.iterator(); iterator.hasNext();) {
int currentWinner = iterator.next();
if (currentWinner==1) {
switch ((String) firstPlayerScore.getText()) {
case "40":
firstPlayerScore.setText("30");
break;
case "A":
firstPlayerScore.setText("40");
break;
case "0":
firstPlayerScore.setText("0");
break;
default:
int currentScore = Integer.parseInt((String) firstPlayerScore.getText());
firstPlayerScore.setText(String.valueOf(currentScore - 15));
break;
}
} else if (currentWinner==2) {
switch ((String) secondPlayerScore.getText()) {
case "40":
secondPlayerScore.setText("30");
break;
case "A":
secondPlayerScore.setText("40");
break;
case "0":
secondPlayerScore.setText("0");
break;
default:
int currentScore = Integer.parseInt((String) secondPlayerScore.getText());
secondPlayerScore.setText(String.valueOf(currentScore - 15));
break;
}
break;
}
currentPlayer.remove(0);
The problem is that I'm obviously facing some java.util.ConcurrentModificationException
issues, since I'm modifying the List.
Any tips to do this?