adamH
adamH

Reputation: 107

Java for loop ArrayList, indexoutofboundsexception

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)

Upvotes: 1

Views: 1072

Answers (1)

Shahid
Shahid

Reputation: 2330

for(int i = 1; i==gamers.size(); i=i+1)

It means the loop will terminate when i==gamers.size() is false. At the first time when i=1, but gamers.size() is greater than 1. It never enters the loop. And returns empty list. When you trying to get one element from an empty list, you are getting the exception.

But I think you want to check i < gamers.size(). It might be:

for(int i = 1; i < gamers.size(); i++)

Again, you don't need winner.clear() in each if block. Rather you could:

private static ArrayList<Game> Winners(ArrayList<Game> gamers) {
    ArrayList<Game> winner = new ArrayList<Game>();

    for (int i = 1; i < gamers.size(); i++) {
        winner.clear();

        if (gamers.get(i).getPoints() > gamers.get(i - 1).getPoints()) {
            winner.add(gamers.get(i));
        } else if (gamers.get(i).getPoints() < gamers.get(i - 1).getPoints()) {
            winner.add(gamers.get(i - 1));
        } else { // last check when points are equal
            winner.add(gamers.get(i));
            winner.add(gamers.get(i - 1));
        }
    }
    return winner;
}

N.B: There is an issue in the approach of your Winners() method. You are iterating over gamers and adding a winner to the list. But every time you are clearing the list. That means you will get only the winner from last two players. Previous winners will be cleared. I think you need to recheck it.

As you are clearing the list, the size of winner list won't be same as the size of play. As a result, you will also get exception in this code.

for(int i = 0; i < play.size(); i=i+1) {
    System.out.println("the Winner is; " + Winners(play).get(i).name);
}

One fix could be:

ArrayList<Game> winnersList = Winners(play);
for(int i = 0; i < winnersList.size(); i++) {
    System.out.println("the Winner is; " + winnersList.get(i).name);
}

Upvotes: 1

Related Questions