jeudi 15 septembre 2016

How to compare ArrayList elements

private static ArrayList<Game> theWinnerIs(ArrayList<Game> gamer){
    ArrayList<Game> winner = new ArrayList<Game>();
    ArrayList<Integer> points = new ArrayList<Integer>();
    for(int i = 0; i<gamer.size(); i=i+1){
        int x = gamer.get(i).getPoints();
        winner.add(x);
    }

    return winner;
}

I have unknown amounts of elements in ArrayList gamer. what i want to do is that, i want to loop trough every element in ArrayList gamer, and call a method on each on of them called getpoints();, to get their score. Then i want to compare the score on each one of these elements, and store the element with the most score in the winner ArrayList, and then return winner. if 2 or more elements share the most score, i want to store them in the winner Arraylist. hence the reason i use Arraylist, because i dont know how many winners.

i know i must use if statements to compare the elements. But i dont know how to start comparing them. how do i get each of their values and store it in if - statements. For simplicity lets say number of elements are 3. otherwise its unknown.

Aucun commentaire:

Enregistrer un commentaire