public static void main(String[] args){
........................
for(int i = 0; i<play.size(); i=i+1){
System.out.println("the Winner is; " +Winners(play).get(i).name);
}
}
This is the main method. i didnt write down Everything here, because its unnecesary. basically what i am doing, is that i am Calling Winners method that takes ArrayList<Game> as argument. play is of type ArrayList<Game>. on that i want to get the elements stored inside and get their name. so that the winners name is shown on screen. i for loop it because there can be several winners. depends on how many shares the same score.
private static ArrayList<Game> Winners(ArrayList<Game> gamers){
ArrayList<Game> winner = new ArrayList<Game>();
for(int i = 1; i==gamers.size(); i=i+1){
if(gamers.get(i).getPoints()>gamers.get(i-1).getPoints()){ winner.clear();
winner.add(gamers.get(i));
}
else if(gamers.get(i).getPoints()<gamers.get(i-1).getPoints()){ winner.clear();
winner.add(gamers.get(i-1));
}
else if(gamers.get(i).getPoints()==gamers.get(i-1).getPoints()){ winner.clear();
winner.add(gamers.get(i));
winner.add(gamers.get(i-1));
}
}
return winner;
}
the Winners method returns an Arraylist<Game>, which is where the gamer or gamers with top score are stored.
i loop through each on of the gamers and compare their points to each other. the one who has the most score gets stored in the Winner arraylist.
i clear the winner arraylist all the time some elements goes inside, because i only want the top Points stored there.
My issue is, i dont know if i am doing it correct. because i am getting error on
on the System.out.println("the Winner is; " +Winners(play).get(i).name);.
it says index 0 size 0 (indexoutofboundsexception)
for(int i = 1; i==gamers.size(); i=i+1){doesn't do what I think that you think it does :)i++instead ofi=i+1When the termination expression evaluates to false, the loop terminates.