我的java程序正在获取IndexOutOfBoundsException

时间:2014-04-22 10:42:19

标签: java runtime-error indexoutofboundsexception

当我的程序进入我的代码的这一部分时,它会收到一个运行时错误。首先我知道它根本没有缩进。我忘了这样做所以我知道这是一个痛苦的阅读。这个方法传递了7个数组列表,其中两个是我自己的课程。

public static void leaderboards(ArrayList<String> teamName, ArrayList<Outcome> aOutcome, ArrayList<Fixture> aFixture, ArrayList<Integer> awayScore, ArrayList<Integer> homeScore, ArrayList<Integer> awayNum, ArrayList<Integer> homeNum) {
    int tem, temps;
    ArrayList<Leaderboard> aLeaderboard = new ArrayList<Leaderboard>();
    for (int counter = 0; counter < teamName.size(); counter++) {
        Leaderboard aLeaderboardEntry = new Leaderboard();
        aLeaderboard.add(aLeaderboardEntry);
    }
    for (int counter = 0; counter < aOutcome.size(); counter++) {
        tem = (aFixture.get(counter)).getHomeNumber();
        temps = (aFixture.get(counter)).getAwayNumber();
        (aLeaderboard.get(tem - 1)).setGamesPlayed();
        (aLeaderboard.get(temps - 1)).setGamesPlayed();
    }
    ArrayList<Results> aResult = new ArrayList<Results>();
    for (int counter = 0; counter < aOutcome.size(); counter++) {
        Results aResultsEntry = new Results();
        aResult.add(aResultsEntry);
        (aResult.get(counter)).setFixtureNumber(counter + 1);
        (aResult.get(counter)).setHomeNumber(homeNum.get(counter));
        (aResult.get(counter)).setAwayNumber(awayNum.get(counter));
        (aResult.get(counter)).setHomeScore(homeScore.get(counter));
        (aResult.get(counter)).setAwayScore(awayScore.get(counter));
    }
    for (int counter = 0; counter < aResult.size(); counter++) {
        if (((aResult.get(counter)).getHomeScore()) > ((aResult.get(counter)).getAwayScore())) {
            (aLeaderboard.get((aResult.get(counter)).getHomeNumber())).sethomeWins();
            (aLeaderboard.get((aResult.get(counter)).getHomeNumber())).setTotalPoints(3);
            (aLeaderboard.get((aResult.get(counter)).getAwayNumber())).setawayLosses();
        } else if (((aResult.get(counter)).getHomeScore()) == ((aResult.get(counter)).getAwayScore())) {
            (aLeaderboard.get((aResult.get(counter)).getHomeNumber())).sethomeDraws();
            (aLeaderboard.get((aResult.get(counter)).getHomeNumber())).setTotalPoints(1);
            (aLeaderboard.get((aResult.get(counter)).getAwayNumber())).setawayDraws();
            (aLeaderboard.get((aResult.get(counter)).getAwayNumber())).setTotalPoints(1);
        } else if (((aResult.get(counter)).getHomeScore()) < ((aResult.get(counter)).getAwayScore())) {
            (aLeaderboard.get((aResult.get(counter)).getHomeNumber())).setHomeLosses();
            (aLeaderboard.get((aResult.get(counter)).getAwayNumber())).setawayWins();
            (aLeaderboard.get((aResult.get(counter)).getAwayNumber())).setTotalPoints(3);
        }
    }
    for (int counter = 0; counter < aResult.size(); counter++) {
        (aLeaderboard.get((aResult.get(counter)).getHomeNumber())).setHomeGoalsFor((aResult.get(counter)).getHomeScore());
        (aLeaderboard.get((aResult.get(counter)).getHomeNumber())).setHomeGoalsAgainst((aResult.get(counter)).getAwayScore());
        (aLeaderboard.get((aResult.get(counter)).getAwayNumber())).setAwayGoalsFor((aResult.get(counter)).getAwayScore());
        (aLeaderboard.get((aResult.get(counter)).getAwayNumber())).setAwayGoalsAgainst((aResult.get(counter)).getHomeScore());
    }
    for (int counter = 0; counter < aLeaderboard.size(); counter++) {
        int goalsFor = (aLeaderboard.get(counter)).getHomeGoalsFor() + (aLeaderboard.get(counter)).getAwayGoalsFor();
        int goalsAgainst = (aLeaderboard.get(counter)).getHomeGoalsAgainst() + (aLeaderboard.get(counter)).getAwayGoalsAgainst();
        int goalDifference = goalsFor - goalsAgainst;
        (aLeaderboard.get(counter)).setGoalsDifference(goalDifference);
    }
    System.out.println("     Positon     Team     Games Played    Home Wins   Home Draws   Home Losses  Home Goals For   Home Goals Against   Away Wins   Away Draws  Away Losses  Away Goals For    Away Goals Against   Goal Difference    Total Points");
}

for(int counter = 0; counter<teamName.size();counter++) {
    System.out.print((counter + 1) + "   " + teamName.get(counter) + "   " + (aLeaderboard.get(counter)).getGamesPlayed() + "   " + (aLeaderboard.get(counter)).getHomeWins() + "   " + (aLeaderboard.get(counter)).getHomeDraws() + "   ");
    System.out.print((aLeaderboard.get(counter)).getHomeLosses());
    System.out.println();
}

错误在这一行:

(aLeaderboard.get((aResult.get(counter)).getAwayNumber())).setawayWins();

如果您需要代码的任何其他部分,例如类本身,请问 这是它产生的错误

> D:\>java Project4 Exception in thread "main"
> java.lang.IndexOutOfBoundsException: Index: 20, Size: 20
>         at java.util.ArrayList.rangeCheck(Unknown Source)
>         at java.util.ArrayList.get(Unknown Source)
>         at Project4.leaderboards(Project4.java:198)
>         at Project4.main(Project4.java:114)
> 
> D:\>

编辑: Away wins是在Leaderboards类中声明的int

public int awayWins;

设置胜利是这种方法

public void setawayWins()
  {
  awayWins++;
  }

2 个答案:

答案 0 :(得分:2)

问题可能出在(aResult.get(counter)).getAwayNumber()。它的值为20,aLeaderboard列表没有该索引。

答案 1 :(得分:0)

我认为你要从ArrayList “aLeaderboard”中找出margin元素

示例 -

  

你的arraylist包含5个元素{0,1,2,3,4},你将从arraylist中检索第6个元素。

不幸的是,由于跟随vars的不清楚,我无法提供解决方案

  • aLeaderboard
  • aResult
  • getAwayNumber
  • setawayWins