简体   繁体   English

获取ArrayIndexOutOfBoundsException并且不确定为什么

[英]Getting ArrayIndexOutOfBoundsException and not sure why

I am learning about object oriented concepts right now. 我现在正在学习面向对象的概念。 I wrote a simple class to accept user input scores but I am getting an out of bounds exception and I'm not sure why! 我编写了一个简单的类来接受用户输入的分数,但是却出现了异常,我不确定为什么! I don't see why this would be accessing indexes over than 4? 我不明白为什么这会访问超过4个索引? Here is the code: 这是代码:

The HighScores class which I am instantiating 5 objects into an array: 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;
    }
}

The program manipulating HighScore objects: 该程序处理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());
        }

    }

}

i guess below line is issue 我想下面是问题

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

try updating sort function as below 尝试更新排序功能如下

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

您在这里增加i而不是j。

I think that the problem is when the loop ends it means that i is no longer smaller then scores.length . 我认为问题在于循环结束时,这意味着i不再比scores.length小。 which means that you are basically checking the of bound when you exit the loop below the line: 这意味着当您退出该行下面的循环时,您基本上正在检查边界的:

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

The problem here is that you are incrementing same vairable i in outer and inner loop. 这里的问题是,您要在外循环和内循环中递增相同的可变i Your outer loop runs 4 times but inner loop increments the value if i further. 您的外循环运行4次,但如果i进一步执行,则内循环会增加该值。 Use a different variable in inner for loop 在内部for循环中使用其他变量

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

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

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