0

您好我目前正在尝试创建一个扫雷游戏,我有我的 gui 并且正在使用 2d 数组来存储一个位置是否有地雷,但是当我尝试让游戏结束以点击一个地雷时使用这个代码:

if (board[row][col] == 1) {
    return GameStatus.Lost; }  
else {
    return GameStatus.Continue;
 }

我得到错误

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10
at Game.getGameStatus(Game.java:55)
at MineSweeperPanel$ButtonListener.actionPerformed(MineSweeperPanel.java:71)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
4

3 回答 3

0

检查您调用的边界rowcol。例如,如果您有 25 行和列并且您指的是board[25][25],则这超出了数组的范围。尽管行数的总大小为 25,但在数组中,索引将从025-1

于 2012-02-11T03:07:49.907 回答
0

数组索引超出范围意味着您的数组中有(比如说)10 个元素,但是您试图访问(比如说)第 11 个元素 - 它只是不存在。

完整性检查 - 数组从 0 开始索引,您的值是否在 1 中rowcol从 1 开始索引?

于 2012-02-11T03:08:10.343 回答
0

什么时候发生?

当您尝试访问索引超过其长度的数组时,会发生越界异常。java数组的最大索引是(长度-1)例如:

String [] stringArray = new String[10];
stringArray[10]
// the code above will produce an out of bounds exception, because the it bigger than length -1, which is 10 - 1 = 9.

如果您不知道数组的大小或长度,可以从stringArray.length.

如何处理?

您应该确保您的程序不会访问索引大于长度的数组 - 1。例如:

for(int i=0;i<stringArray.lenght;i++) {
    //write your code here
}

上面的代码将保证stringArray永远不会超过其最大索引被访问。

你的案例

在您的情况下,您必须定义数组限制并尝试访问超出定义限制范围的数组数据。

另请阅读内容以获取更多信息...

具有越界异常的二维数组示例

于 2012-02-11T03:42:53.837 回答