获取ArrayIndexOutOfBoundsException并且不确定原因

时间:2013-10-13 05:19:05

标签: java arrays object indexoutofboundsexception

我正在学习面向对象的概念。我写了一个简单的类接受用户输入分数,但我得到一个越界异常,我不知道为什么!我不明白为什么这会访问超过4的索引?这是代码:

我正在将5个对象实例化为数组的HighScores类:

public class HighScores
{
    String name;
    int score;

    public HighScores()
    {
        this.name = "";
        this.score = 0;
    }
    public HighScores(String name, int score)
    {
        this.name = name;
        this.score = score;
    }

    void setName(String name)
    {
        this.name = name;
    }

    String getName()
    {
        return this.name;
    }

    void setScore(int score)
    {
        this.score = score;
    }

    int getScore()
    {
        return this.score;
    }
}

操纵HighScore对象的程序:

import java.util.Scanner;

public class HighScoresProgram
{

    public static void main(String[] args)
    {
        HighScores[] highScoreObjArr = new HighScores[5];

        for (int i = 0; i < highScoreObjArr.length; i++)
        {
            highScoreObjArr[i] = new HighScores();
        }
        initialize(highScoreObjArr);
        sort(highScoreObjArr);
        display(highScoreObjArr);
    }

    public static void initialize(HighScores[] scores)
    {
        Scanner keyboard = new Scanner(System.in);
        for(int i = 0; i < scores.length; i++)
        {
            System.out.println("Enter the name for for score #" + (i+1) + ": ");
            String temp = keyboard.next();
            scores[i].setName(temp);
            System.out.println("Enter the the score for score #" + (i+1) + ": ");
            scores[i].setScore(keyboard.nextInt());
        }

    }

    public static void sort(HighScores[] scores)
    {
        for(int i = 0; i < scores.length; i++)
        {
            int smallest = i;

            for (int j = i; i < scores.length; i++)
            {
                if (scores[j].getScore() < scores[smallest].getScore())
                    smallest = j;
            }

            HighScores temp = scores[i];
            HighScores swap = scores[smallest]; //This is where I'm getting the out of bounds exception.
            scores[i] = swap;
            scores[smallest] = temp;

        }
    }

    public static void display(HighScores[] scores)
    {
        System.out.println("Top Scorers: ");
        for(int i = 0; i < scores.length; i++)
        {
            System.out.println(scores[i].getName() + ": " + scores[i].getScore());
        }

    }

}

5 个答案:

答案 0 :(得分:5)

我想下面的行是问题

for (int j = i; i < scores.length; i++)

尝试更新排序功能,如下所示

 public static void sort(HighScores[] scores)
    {
        for(int i = 0; i < scores.length; i++)
        {
            int smallest = i;

            for (int j = i; j < scores.length; j++)
            {
                if (scores[j].getScore() < scores[smallest].getScore())
                    smallest = j;
            }

            HighScores temp = scores[i];
            HighScores swap = scores[smallest]; //This is where I'm getting the out of bounds exception.
            scores[i] = swap;
            scores[smallest] = temp;

        }
    }

答案 1 :(得分:2)

 for (int j = i; i < scores.length; i++)

你在这里递增i而不是j。

答案 2 :(得分:1)

我认为问题是当循环结束时意味着i不再小于scores.length。这意味着当你退出该行下方的循环时,你基本上是检查了界限:

for (int j = i; i < scores.length; i++)

答案 3 :(得分:0)

这里的问题是你在外循环和内循环中递增相同的可变i。您的外部循环运行4次,但如果进一步i,内部循环会增加该值。在内部循环中使用另一个变量

答案 4 :(得分:0)

for (int j = i; j < scores.length; j++)