简体   繁体   English

Java for 循环ArrayList,indexoutofboundsexception

[英]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.基本上我在做什么,是我正在调用将ArrayList<Game>作为参数的Winners方法。 play is of type ArrayList<Game> . playArrayList<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. Winners方法返回一个Arraylist<Game> ,这是一个或多个得分最高的玩家的存储位置。 i loop through each on of the gamers and compare their points to each other.我遍历每个gamers并将他们的points相互比较。 the one who has the most score gets stored in the Winner arraylist.得分最高的那个被存储在Winner数组列表中。

i clear the winner arraylist all the time some elements goes inside, because i only want the top Points stored there.我总是清除winner数组列表,因为我只希望将最高点存储在那里。

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);因为我在System.out.println("the Winner is; " +Winners(play).get(i).name);上出错了. . it says index 0 size 0 (indexoutofboundsexception)它说 index 0 size 0 (indexoutofboundsexception)

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

It means the loop will terminate when i==gamers.size() is false.这意味着当i==gamers.size()为 false 时循环将终止。 At the first time when i=1, but gamers.size() is greater than 1. It never enters the loop.第一次当 i=1 时,但是 gamers.size() 大于 1。它永远不会进入循环。 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() .但我认为你想检查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.同样,您不需要在每个 if 块中使用winner.clear() 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;
}

NB: There is an issue in the approach of your Winners() method.注意:您的Winners()方法的方法存在问题。 You are iterating over gamers and adding a winner to the list.您正在遍历gamers并将winner添加到列表中。 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 .当您清除列表时, winner列表的大小将与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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM