【问题标题】:GameOfLife IndexOutOfBounds Error-handlingGameOfLife IndexOutOfBounds 错误处理
【发布时间】:2013-12-05 05:51:12
【问题描述】:

所以,我正在做一个任务,让课程接收为康威的生命游戏设置的文本文件。我已经编写了所有内容,但是由于我对错误处理很烂,因此很难对其进行测试。我已经阅读了有关 try、catch、throw 等的 java 教程页面。我不明白,如果我能得到一些解决 IndexOutOfBounds 错误的东西,它将为我节省很多时间。

public void computeNextGeneration(int generation) {
    int aliveCount;
    tempBoard = new char[Column][Row];
    int generationCount = 1;
    System.out.print("Generation" + generationCount);
    print();
    do {
        for (int i = 0; i < Row; i++) {
            for (int j = 0; j < Column; j++) {
                aliveCount = 0;
                try {
                    if (board[Row - 1][Column - 1] == 'X') {
                        aliveCount++;
                    }
                    if (board[Row - 1][Column] == 'X') {
                        aliveCount++;
                    }
                    if (board[Row - 1][Column + 1] == 'X') {
                        aliveCount++;
                    }
                    if (board[Row][Column - 1] == 'X') {
                        aliveCount++;
                    }
                    if (board[Row][Column + 1] == 'X') {
                        aliveCount++;
                    }
                    if (board[Row + 1][Column - 1] == 'X') {
                        aliveCount++;
                    }
                    if (board[Row + 1][Column + 1] == 'X') {
                        aliveCount++;
                    }
                    if (board[Row + 1][Column + 1] == 'X') {
                        aliveCount++;
                    }
                    if (board[i][j] == 'X') {
                        if (aliveCount < 2) {
                            setCell(j, i, 0);
                        }
                        if (aliveCount > 2) {
                            setCell(j, i, 0);
                        } else {
                            setCell(j, i, 1);
                        }
                    }
                    if (board[i][j] == '0') {
                        if (aliveCount == 3) {
                            setCell(j, i, 1);
                        }
                    }
                } catch (IndexOutOfBoundsException e) {
                }
                throw new IndexOutOfBoundsException();
            }
            board = tempBoard;
            generationCount++;
            System.out.print("Generation" + generationCount);
            print();
            System.out.println();
            generation--;
        }
    } while (generation > 1);
}

第一种情况,在二维数组的边缘会给出第一个错误。我想如果我把检查相邻数组索引的代码放在一起......老实说,我只是把代码放在一起,就像在黑暗中拍摄一样。如果我能得到一个与我的问题类似的示例,任何可以说明处理 IndexOutOfBounds 错误的示例,我将不胜感激。

【问题讨论】:

  • 不要将 try catch 用于已知发生的行为,而是用于意外行为,例如服务器在传输过程中断开连接。对这类事情使用 if 语句。
  • 你如何声明board数组?
  • 我刚刚在我的 GameOfLife 类中发布了一个方法,其中场板被实例化。 @Masud

标签: java error-handling


【解决方案1】:

将 8 个 if 语句从 try-catch 场景中取出,转而专注于检查 board 的索引是否正确。换句话说,在访问board[Row - 1][Column - 1] 之前,请检查0 &lt;= Row-1 &lt;= NumOfRows0 &lt;= Column-1 &lt;= NumOfColumns

编辑

更明确的观点。

for (int i = 0; i < NumOfRows; i++) {
            for (int j = 0; j < NumOfColumns; j++) {
                aliveCount = 0;
                if (i-1 >= 0 && i-1 <= NumOfRows) {//This case checks the cell to the left
                    aliveCount++;
                }
                //Other cases

【讨论】:

  • 请注意,您实际上不能像那样链接&lt;=。要表达 a ≤ b ≤ c,您需要 a &lt;= b &amp;&amp; b &lt;= c
  • 我希望我的意思在我的编辑中更清楚,@DaoWen。
【解决方案2】:

您正在尝试访问board[Row + 1][Column + 1],这会引发ArrayIndexOutOfBound 异常。

类似的board[Row][Column + 1]board[Row + 1][Column] 会引发异常。因为Row + 1Column + 1 位置的元素没有被初始化。

【讨论】:

    【解决方案3】:

    关于这个的一些想法......

    首先,您使用

    访问了错误的数组元素
    for (int i = 0; i < Row; i++) {
        for (int j = 0; j < Column; j++) {
            ...
            // Wrong Indices, possible Exception
            //        vvvvvvv vvvvvvvvvvv
            if (board[Row - 1][Column - 1] == 'X') {
                aliveCount++;
            }
            ...
        }
    }
    

    像这样,您总是检查相同的元素(最后一行,最后一列)。但是您想检查当前元素旁边的元素。所以像

    // [i][j] -> Current Element
    // [i-1][j-1] -> Element on the top left
    if (board[i - 1][j - 1] == 'X') { 
        aliveCount++;
    }
    

    应该使用。但是,这可以并且将会抛出 IndexOutOfBoundsException。所以你必须检查ij 的值是否在正确的范围内。所以添加检查以确保有效值:

    if (0 <= (i - 1) && (i - 1) <= Row && 0 <= (j - 1) && (j - 1) <= Column) {
        if (board[i - 1][j - 1] == 'X') { 
            aliveCount++;
        }
    } else {
        // TODO: What to do with the Edges? Assume 0/1? Wrap around?
    }
    

    但是,此代码在您的循环中重复了 8 次,这将难以阅读、难以调试并且容易出错。所以最好把它移到一个单独的函数中。类似的东西

    public boolean cellIsAlive(char[][] board, int row, int col)
    {
        if (0 <= (row - 1) && (row - 1) <= Row && 0 <= (col- 1) && (col- 1) <= Column) {
            if (board[row - 1][col- 1] == 'X') { 
                return true;
            }
        } else {
            // TODO: What to do with the Edges? Assume 0/1? Wrap around?
            return false;
        }
        return false;
    }
    
    ...
    if (cellIsAlive(board, i, j)) {
        aliveCount++;
    }
    

    另一个注意事项:实际上,如果单元格存在,您不需要任何检查。你知道网格的大小,现在你知道不同的情况:板的中间,第一行,最后一行,第一列,第二列,角元素。因此,您可以单独处理这些情况,而不是遍历整个电路板。但这对你的例子来说可能有点过头了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      • 2023-01-06
      • 2017-12-24
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 2016-06-02
      相关资源
      最近更新 更多