Java GameOfLife IndexOutOfBounds错误处理

Java GameOfLife IndexOutOfBounds错误处理,java,error-handling,Java,Error Handling,所以,我正在做一个作业,让一个类接收为康威的生活游戏设置的文本文件。我已经写了所有的东西,但是很难测试,因为我的错误处理能力很差。我已经阅读了关于try、catch、throw等的java教程页面。我不理解它,如果我能找到一些解决IndexOutOfBounds错误的方法,这将节省我很多时间 public void computeNextGeneration(int generation) { int aliveCount; tempBoard = new char[Column

所以,我正在做一个作业,让一个类接收为康威的生活游戏设置的文本文件。我已经写了所有的东西,但是很难测试,因为我的错误处理能力很差。我已经阅读了关于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);
}
public void computeNextGeneration(int-generation){
国际aliveCount;
tempBoard=新字符[列][行];
int generationCount=1;
系统输出打印(“生成”+生成计数);
打印();
做{
对于(int i=0;i2){
setCell(j,i,0);
}否则{
setCell(j,i,1);
}
}
如果(电路板[i][j]=“0”){
如果(aliveCount==3){
setCell(j,i,1);
}
}
}catch(IndexOutOfBoundsException e){
}
抛出新的IndexOutOfBoundsException();
}
板=临时板;
generationCount++;
系统输出打印(“生成”+生成计数);
打印();
System.out.println();
世代--;
}
}而(世代>1);
}

第一种情况是,位于2d阵列的边缘将产生第一个错误。我想如果我把检查相邻数组索引的代码。。。老实说,我只是在暗中拼凑代码。如果我能得到一个与我的问题类似的例子,任何能举例说明如何处理IndexOutOfBounds错误的例子,我都会非常感激。

您正在尝试访问
board[Row+1][Column+1]
,它抛出
ArrayIndexOutOfBound
异常


类似地,
board[Row][Column+1]
board[Row+1][Column]
抛出异常。因为位于
行+1
列+1
位置的元素未初始化。

将8
if
语句从try-catch场景中去掉,而将重点放在检查
板的索引是否正确。换句话说,在访问
board[Row-1][Column-1]
之前,请检查
0关于这一点的一些想法

首先,您正在使用访问错误的数组元素

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++;
        }
        ...
    }
}
应该使用。然而,这可能并将引发IndexOutOfBoundsException。因此,您必须检查
i
j
的值是否在正确的范围内。因此,添加检查以确保有效值:

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?
}

if(0不要对已知发生的行为使用try-catch,而是对意外的行为使用try-catch,例如在传输过程中服务器断开连接。对这类事情使用if语句。如何声明
board
array?我刚刚在GameOfLife类中发布了一个方法,其中字段board是实例化的。@MasudJust注意,您不能“我希望我的意思从我的编辑中更清楚,”道文说。
// [i][j] -> Current Element
// [i-1][j-1] -> Element on the top left
if (board[i - 1][j - 1] == 'X') { 
    aliveCount++;
}
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?
}
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++;
}