0

I have an ArrayList <Players> players = new ArrayList();

In which I have a method where I have set integers to them. Using players.get(i).getWincounter;

I can find their specific integers, however now I want to remove all the lowest integers. ex (2,1,1,1) 1s are the lowest however i only remove 1 of them, not all.

This is what I have tired:

int current = 0;
int min = 900;
// ArrayList<Integer> min = new ArrayList <Integer>();
if (players.size() >= 2) {
    for (int i = 0; i < players.size(); i++) {
        current = players.get(i).getWinCounter();
        if (current < min) {
            min = current;
        }
    }
    // System.out.println(min);
    removePlayer(players, min);
    checkLeftPlayers(players);
}

RemovePlayer method contains the following:

public void removePlayer(ArrayList<Player> players, int min) {
    System.out.println(min);
    for (int i = 0; i < players.size(); i++) {
        if (players.get(i).getWinCounter() == min) {
            players.remove(players.get(i));
        }
    }
    System.out.println("====================================");
    System.out.println("NEW STAGE");
    checkLeftPlayers(players);
}

There is something wrong with my remove method and I can't figure out what. Help is greatly appreciated!

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Anika
  • 103
  • 1
  • 8
  • If you want to be able to understand your own code, and if you want us to be able to understand it, indent it correctly. Your IDE can format it with a single keyboard shortcut. – JB Nizet Nov 17 '19 at 20:27
  • 2
    Every time you remove an element from the `ArrayList`, the index changes (so the next element which was `i + 1` is now `i`), you then increment `i` which means you skip over it – MadProgrammer Nov 17 '19 at 20:27
  • 1
    Use an Iterator to iterate through the list, and use the iteratr's remove method to remove the current element. or simpler, use the removeIf() method. – JB Nizet Nov 17 '19 at 20:29
  • You can also use a `while()` loop and increment the loop counter in the `else` part of this condition `if (players.get(i).getWinCounter() == min)` fails. So that the loop counter will not increase if any item get removed. – Kousik Nov 17 '19 at 20:32

0 Answers0