【问题标题】:How to initialize a array object which is passed as parameter in java?如何初始化在java中作为参数传递的数组对象?
【发布时间】:2014-08-15 13:17:14
【问题描述】:

我不知道我的代码发生了什么,但在下面的程序中,我在UpdateHighScoreRecords() 函数中传递了一个对象数组,它可以包含或不包含一些值。如果数组对象大小为零,我必须初始化highScoreRecords[0],但它显示ArrayIndexOutOfBoundsException。你能解决这个问题吗?

/**
 * The GameRecord class represents a record for the a game play, including the name of the player, the level of the game play and the score obtained.
 */

    public class GameRecord {

    /** The name of the player. **/
    private String name;

    /** The level of the game play. **/
    private int level;

    /** The score of the game play. **/
    private int score;

    /**
     * Constructs a game record with a certain name, level and score.
     * @param name the name of the player.
     * @param level the level of the game play.
     * @param score the score of the game play.
     */
    public GameRecord(String name, int level, int score) {

        this.name = name;
        this.level = level;
        this.score = score;
    }

    /**
     * Returns the name of the player in the record.
     * @return the name of the player in the record.
     */
    public String getName() {
        return name;
    }

    /**
     * Sets the name of the player in the record.
     * @param name the name of the player to be updated to.
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Returns the level of the game play in the record.
     * @return the level of the game play in the record.
     */
    public int getLevel() {
        return level;
    }

    /**
     * Sets the level of the game play in the record.
     * @param level the level of the game play to be updated to.
     */
    public void setLevel(int level) {
        this.level = level;
    }

    /**
     * Returns the score of the game play in the record.
     * @return the score of the game play in the record.
     */
    public int getScore() {
        return score;
    }

    /**
     * Sets the score of the game play in the record.
     * @param score the score of the game play to be updated to.
     */
    public void setScore(int score) {
        this.score = score;
    }
}



    public GameRecord[] updateHighScoreRecords(GameRecord[] highScoreRecords, String name, int level, int score) 
    {
        // write your code after this line
        if(highScoreRecords.length == 0 )
        {
            highScoreRecords[0] = new GameRecord(name, level, score);

            return highScoreRecords;
        }
        else if(highScoreRecords.length < 10)
        {
            int i=0;
            while(i<highScoreRecords.length)
            {
                if(highScoreRecords[i].getName().equals(name))
                {
                    if(highScoreRecords[i].getScore() < score )
                    {
                        highScoreRecords[i].setScore(score);
                        highScoreRecords[i].setLevel(level);
                        break;
                   }
                   break;
                }
                i++;
            }
            if(i == highScoreRecords.length)
            {
                highScoreRecords[i].setName(name);
            highScoreRecords[i].setScore(score);
            highScoreRecords[i].setLevel(level);
            }
        }
        else if (highScoreRecords.length == 10)
        {
             int i=0;
            while(i<highScoreRecords.length)
            {
                if(highScoreRecords[i].getName().equals(name))
                {
                    if(highScoreRecords[i].getScore() < score )
                    {
                        highScoreRecords[i].setScore(score);
                        highScoreRecords[i].setLevel(level);
                        break;
                   }
                   break;
                }
                i++;
            }
            if(i == highScoreRecords.length)
            {
            highScoreRecords[9].setName(name);
            highScoreRecords[9].setScore(score);
            highScoreRecords[9].setLevel(level);
            }
        }

        return highScoreRecords;
    }

【问题讨论】:

  • 你对ArrayIndexOutOfBoundException的理解是什么?
  • 您的数组长度为零,但您试图在其中存储一个对象。那不会飞的。
  • 特别是:if (array.length == 0) { array[0] = ... }永远不会起作用。是时候修改数组了。
  • 如果highScoreRecordsnull,它应该是highScoreRecords=new GameRecord[1];

标签: java arrayobject


【解决方案1】:

数组有一个fixed size after initialization。这不是 JavaScript。初始化为 GameRecords[] gameRecords = new GameRecords[5]; 的数组将始终具有从 0...4 的索引开始的 5 大小。

您尝试使用的是List。因此,您应该改用以下构造:

List<GameRecord> gameRecords = new ArrayList<GameRecord>(); //java.util.List
gameRecords.add(new GameRecord(...));
gameRecords.get(0);
for(GameRecord gameRecord : gameRecords)
{
    ....
}

【讨论】:

  • OP 想要数组,而不是集合。并且,如果您检查 ArrayList.add() 它会检查支持的数组的容量,如果超出,则进行数组复制。同样的事情,只是我们没有看到。
  • 如果他动态地尝试改变数组的大小,他当然不想要一个数组。如果他真的想根据数组的长度改变逻辑,那么他应该把它作为不同的类而不是把所有东西都混合到GameRecord中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多