Java 获取ArrayIndexOutOfBoundsException并不确定原因

Java 获取ArrayIndexOutOfBoundsException并不确定原因,java,arrays,object,indexoutofboundsexception,Java,Arrays,Object,Indexoutofboundsexception,我现在正在学习面向对象的概念。我写了一个简单的类来接受用户输入分数,但我得到了一个越界异常,我不知道为什么!我不明白为什么要访问超过4个的索引?代码如下: HighScores类,我正在将5个对象实例化为一个数组: public class HighScores { String name; int score; public HighScores() { this.name = ""; this.score = 0; }

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

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

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());
        }

    }

}
import java.util.Scanner;
公开课高分计划
{
公共静态void main(字符串[]args)
{
HighScores[]highScoreObjArr=新的高分[5];
for(int i=0;i
我想下面这行就是问题所在

for (int j = i; i < scores.length; i++)
for (int j = i; i < scores.length; i++)
for(int j=i;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;

        }
    }
公共静态无效排序(高分[]分)
{
for(int i=0;i
我想下面这行就是问题所在

for (int j = i; i < scores.length; i++)
for (int j = i; i < scores.length; i++)
for(int j=i;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;

        }
    }
公共静态无效排序(高分[]分)
{
for(int i=0;i
这里的问题是,您在外循环和内循环中递增相同的VARABLE
i
。外部循环运行4次,但如果
i
进一步,内部循环会增加值。在内部for循环中使用不同的变量

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

我认为问题在于循环结束时,这意味着
I
不再小于
分数。长度
。这意味着,当您退出该行下方的循环时,基本上是在检查边界的边界:

for(int j=i;i
我认为问题在于当循环结束时,这意味着
I
不再小于
分数。长度
。这意味着,当您退出该行下方的循环时,基本上是在检查边界的边界:

for(int j=i;i
for(int j=i;i
这里你是在增加i而不是j

for(int j=i;ifor (int j = i; j < scores.length; j++)
这里你是在增加i而不是j

for(int j=i;jfor (int j = i; j < scores.length; j++)
for(int j=i;j
哇,我觉得自己像个白痴!我一直在绞尽脑汁想弄明白。谢谢大家的帮助!我很高兴你能解决你的问题。将来,如果您遇到异常,请发布整个消息以使调试更容易,并且最好注意异常是从哪一行抛出的。哇,我觉得自己像个白痴!我一直在绞尽脑汁想弄明白。谢谢大家的帮助!我很高兴你能解决你的问题。将来,如果您遇到异常,请发布整个消息以使调试更容易,并且最好注意引发异常的行。