为什么我在这里收到错误“ java.lang.ArrayIndexOutOfBoundsException:2”

时间:2019-04-28 08:04:17

标签: java cycle

newGame包含具有参数(id,点)的玩家列表。 因此,我需要对得分进行排序,并按照得分更高的球员的ID来获得新的ArrayList。

ArrayList<Integer> getScores() {
        final ArrayList<Integer> myArrayList = new ArrayList<>();
        final int[] array = new int[this.newGame.size()];
        for (int i = 0; i < (this.newGame.size()); i++) {
            array[i] = this.newGame.get(i).getScore();
        }
        Arrays.sort(array);
        for (int i = this.newGame.size(); i > 0; i--) {
            for (int j = 0; j < this.newGame.size(); j++) {
                if (array[i] == this.newGame.get(j).getScore()) {
                    myArrayList.add(this.newGame.get(i).getPlayerId());
                    this.newGame.get(i).setScore(-1);
                    break;
                }
            }
        }
        return myArrayList;

    }

0 个答案:

没有答案
相关问题