【问题标题】:Getting ArrayIndexOutOfBoundsException and not sure why获取 ArrayIndexOutOfBoundsException 不知道为什么
【发布时间】:2013-10-13 05:19:05
【问题描述】:

我现在正在学习面向对象的概念。我写了一个简单的类来接受用户输入分数,但是我得到了一个越​​界异常,我不知道为什么!我不明白为什么这会访问超过 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());
        }

    }

}

【问题讨论】:

  • 哇,我觉得自己像个白痴!我一直在敲我的头试图弄清楚。感谢大家的帮助!
  • 很高兴您能够解决您的问题。将来,如果您遇到异常,请发布整个消息以使其更易于调试,并最好记下引发异常的行。

标签: java arrays object indexoutofboundsexception


【解决方案1】:

我猜下面一行是问题

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;

        }
    }

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:
       for (int j = i; i < scores.length; i++)
      

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:
          for (int j = i; j < scores.length; j++)
          

          【讨论】:

            猜你喜欢
            • 2020-05-07
            • 2013-11-07
            • 1970-01-01
            • 2013-12-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多