-4

I don't know what is happening to my code but in below program I have a object array passed in UpdateHighScoreRecords() function which can contain some value or not. If array object size is zero I have to initialize the highScoreRecords[0] but it is showing ArrayIndexOutOfBoundsException. Can you solve this problem ?

/**
 * 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;
    }
Jonathan Drapeau
  • 2,610
  • 2
  • 26
  • 32

1 Answers1

1

Arrays have a fixed size after initialization. This is not JavaScript. An array initialized as GameRecords[] gameRecords = new GameRecords[5]; will always have a size of 5 from the indices of 0...4.

What you're trying to use is a List. So you should use the following construct instead:

List<GameRecord> gameRecords = new ArrayList<GameRecord>(); //java.util.List
gameRecords.add(new GameRecord(...));
gameRecords.get(0);
for(GameRecord gameRecord : gameRecords)
{
    ....
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • OP wants array, not a collection. and, if you check ArrayList.add() it checked the backed array's capacity, and if exceeded, do array copy. same thing, just we don't see. – Kent Aug 15 '14 at 13:23
  • 1
    He certainly doesn't want an array if he's dynamically trying to alter its size. If he actually wants to alter the logic based on the length of the array, then he should just have it as different classes rather than mixing everything into `GameRecord`. – EpicPandaForce Aug 15 '14 at 13:24