0

I have a Listof 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?

Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Fares
  • 893
  • 1
  • 11
  • 24

2 Answers2

0

What you can do is before you create the iterator you can remove the first element at index 0 and treat it as the winner. Then you can iterate the rest and assume they're not the winner.

The ConcurrentModificationException occurs because you're trying to modify the underlying Collection that Iterator was composed by while iterating the Iterator.

                Collections.reverse(currentPlayer);

                Integer winner = currentPlayer.remove(0);

                Iterator<Integer> iterator = currentPlayer.iterator();

                while (iterator.hasNext()) {
                    int nonWinner = iterator.next();

                    // your code
                }
Jason
  • 5,154
  • 2
  • 12
  • 22
0

You live in the modern age where everything is a web search away. JavaSE, in particular, is extremely well documented compared to some other APIs. It is a waste if you choose not to use it. The best advice I can give you, is if it sounds like a problem that someone else in the world would likely have hit before you, then it probably has and is worth some searching.

In the meantime, to make this answer complete, Iterator#remove() is not the same as List#remove(int) or List#remove(Object).

  • Iterator#remove() is to remove the first element of an iterator, exactly what you're asking for.
  • List#remove(int) is to remove the element at the index specified by the parameter.
  • List#remove(Object) is to remove all Objects in the list that match the parameter. This is the one that it seems is the method you're calling.

Now I'm not totally convinced your claims about your code are correct. You claim, that all occurrences of 0 are removed. That shouldn't be what happens at all. What should happen when you provide a primitive int is remove the 0th index of the list. If you were to do list.remove(new Integer(0)) then I would expect the behavior you're describing.

In other words, there should be no autoboxing of the 0 here, because there is a function that expects the primitive already. See Properly removing an Integer from a List (i.e. somebody asked the exact question you're asking now, but has asked it literally 10 years ago)

searchengine27
  • 1,449
  • 11
  • 26