首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >测试2D对象数组中的空值

测试2D对象数组中的空值
EN

Stack Overflow用户
提问于 2018-12-02 04:47:15
回答 1查看 60关注 0票数 1

我正在做一个课堂项目,在那里我必须创建一个游戏。我有两个类,BoardButton和TreasureButton,它们创建按钮;TreasureButton类扩展了BoardButton类。

在我正在处理的类中,我试图创建一个面板,其中包含一个2D数组(10x10),其中包含20个随机选择的TreasureButton类实例,而BoardButton类的其余80个实例。当我运行这个程序时,我会得到一个运行时错误:

代码语言:javascript
复制
java.lang.ArrayIndexOutOfBoundsException: -1157793070
    at GameBoardPanel.<init>(GameBoardPanel.java:46)
    at TreasureGame.<init>(TreasureGame.java:22)
    at TreasureGame.main(TreasureGame.java:33)

抛出错误的行的代码是:

代码语言:javascript
复制
if (gameBoardButtons[row][col] == null)

因为我还没有初始化数组,所以我认为数组中所选索引处的值应该设置为null。任何帮助都将不胜感激。

代码语言:javascript
复制
public class GameBoardPanel extends JPanel
{
    private BoardButton[][] gameBoardButtons;       // BoardButton 2D array that will be used to populate the board
    private final int NUM_TREASURE_BUTTONS = 20;    // Number of treasure buttons that will be added to the array
    private final int NUM_ROWS = 10;                // Number of rows in the array
    private final int NUM_COLS = 10;                // Number of columns in the array

    public GameBoardPanel()
    {
        int treasureButtonInstances = 0;            // Used to count the number of TreasureButton instances

        // Create the 'gameBoardButtons' array and make it a 10x10 array
        gameBoardButtons = new BoardButton[NUM_ROWS][NUM_COLS];

        // Build an object from the Random class that chooses a number between 0-9
        Random randomNumber = new Random(10);

        // Build a while loop that randomly adds 20 different TreasureButton instances to the 'gameBoardButtons' BoardButton array
        while (treasureButtonInstances < NUM_TREASURE_BUTTONS)
        {
            // Obtain two random numbers that will be used to assign a TreasureButton instance to the 'gameBoardButtons' BoardButton array
            int row = randomNumber.nextInt();
            int col = randomNumber.nextInt();

            // If statement that adds an instance of the TreasureButton class if that particular row/column of the 'gameBoardButtons' BoardButton array is empty
            if (gameBoardButtons[row][col] == null)
            {
                // Create an instance of the TreasureButton class and assign it to the particular row/col of our 'gameBoardButtons' BoardButton array
                gameBoardButtons[row][col] = new TreasureButton();

                // Increment the 'treasureButtonInstances' variable, as an instance of the TreasureButton class has been created
                treasureButtonInstances++;
            }// End of the if statement
        }// End of the while loop

        // Build a nested for loop that will populate the rest of the 'gameBoardButtons' BoardButton array
        for (int row = 0; row < NUM_ROWS; row++)
        {
            for (int col = 0; row < NUM_COLS; row++)
            {
                // If statement that will assign an instance of the BoardButton class if that particular row/col of our 'gameBoardButtons" BoardButton array is empty
                if (gameBoardButtons[row][col] == null)
                {
                    // Create an instance of the BoardButton class and assign it to the particular row/col of our 'gameBoardButtons' BoardButton array
                    gameBoardButtons[row][col] = new BoardButton();
                }// End of the if statement
            }//End of the nested for loop
        }// End of the for loop
    }// End of the GameBoardPanel no-arg constructor
EN

回答 1

Stack Overflow用户

发布于 2018-12-02 04:57:35

Random构造函数中的参数是seed,它防止每次生成相同的序列,但与随机整数的范围无关。

在大多数情况下,您不必设置种子,只需使用new Random(),java将自行处理种子。

要将生成的数字限制在0到9,您应该调用randomNumber.nextInt(10)而不是randomNumber.nextInt()

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53577488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档