1

我想在这个条件下做的是确保数组中的对象(中间单元格的邻居)物理上在数组内,所以我想我会让构造函数中的第三个变量(年龄)等于零,思考如果它找不到那个实例变量,它就不会走得更远。但它给了我一个超出范围的-1异常,我不知道如何重写它以避免这种情况。(如果这不是足够的细节,我很抱歉,我可以提供更多,只是问)

所以这是我的代码部分我坚持:

for (int x = 0; x < Grid.columns; x++) {

    for (int y = 0; y < Grid.rows; y++) {

        int nCount = 0;
        int hCount = 0;
        //check neighbors
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                //check if valid and identity
                //STUCK HERE--->   if(i !=0 && j !=0)
                if (board[x + i][y + j].age == 0) {
                    nCount++;
                    if (board[x + i][y + j].getPreviousValue() == 0)
                        hCount++;
                }

            }
        }
        board[x][y].setCurrentValue(hCount / nCount, (nCount - hCount) / nCount);
    }
}
4

4 回答 4

3

您的问题是网格的“边缘单元”,因为您访问数组之外​​的索引。(如 -1 和 array.length)。要获得合理的解决方案,您必须检查这种情况。

    for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {

            int neighbor_x = x + i;
            int neighbor_y = y + j;

            if (neighbor_x < 0 || neighbor_x >= board.length) {
              // Out of Grid
            }

            if (neighbor_y < 0 || neighbor_y >= board[neighbor_x].length) {
              // Out of Grid
            }

            if (board[neighbor_x][neighbor_y].age == 0) {
                nCount++;
                if (board[x + i][y + j].getPreviousValue() == 0)
                    hCount++;
            }

        }
    }

另一种可能性是简单地捕获异常。但是,这可以被认为是不好的风格。我不建议使用此代码:

for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
        try {
            if (board[x + i][y + j].age == 0) {
                nCount++;
                if (board[x + i][y + j].getPreviousValue() == 0)
                    hCount++;
                }
            }
        } catch (IndexOutOfBoundsException e) {
            // Here you know that the exception occured.
            //
            // In this particular case we will not write any code,
            // since the proper handling of this exception is to move
            // on to the next cell.
        }
    }
}

并且作为评论:风格总是主观的,但6级嵌套通常不好。

于 2013-09-20T22:51:33.010 回答
2

在您的情况下,您的数组不能为负值-1。数组从 0 开始,所以当i等于 -1 和j等于 -1 时,你会得到那个错误。您将 -1 添加到 0,从而为您的数组选择获得 -1。保持数字为正。

我也不确定你想在这里做什么。我的意思是如果元素不在数组中,它还会在哪里?如果您想知道项目是否在数组中,可以使用数组方法来查看特定值是否在数组中。如果您要确定是否存在特定值,那是一回事,我只是不清楚您要做什么。

于 2013-09-20T22:49:41.997 回答
1

我的建议是做一些边界检查。

使用您的示例,在第四个(!) for 循环中的第一次,您有x=0, y=0,i=-1j=-1. 这意味着您正在尝试访问board[-1][-1],这显然不存在。解决此问题的一种(不一定是最佳)方法是使用另一个if检查越界条件的语句,例如if ((x+i) >= 0) && ((x+1) < Grid.columns) && ((y+j) >= 0 && (y+j) < Grid.Rows).

问题不在于if(i !=0 && j !=0)部件.. 在于您的索引超出范围这一事实。

顺便说一句,正如评论和其他答案中提到的那样,拥有第四个嵌套for循环并不是一个好主意,并且很容易导致内存或运行时性能问题。在这种特殊情况下,由于您使用第三和第四个嵌套 for 循环最多检查八个点(如果取消注释!=0if 语句则为四个点),您可以尝试仅枚举这八个(或四个)。

另外,如果您有嵌套if语句,如下所示:

if(test1)
    if(test2)
        doSomething();

那么您可以尝试将它们组合成一个 if 语句:

if(test1 && test2)
    doSomething();

这并不总是有效(else想到语句),但它可以帮助您的代码可读。

于 2013-09-20T22:57:42.350 回答
0

有了所有这些复杂的代码,我觉得我无法理解去哪里,我理解的是你想处理二维数组中的一个对象,你可以做这样的事情:

    int[][][] ints = {              
                {{1,2,3},{4,5,6}},  // dimension 0
                {{},{},{7,8,9}}     // dimension 1
                };

    for(int i = 0;i<ints.length;i++){
        for(int t = 0;t<ints[i].length;t++){
            // check if the middle Dimension array is initialized
            if((ints[i][t] != null) && ints[i][t].length > 0){
                for(int z = 0;z<ints[i][t].length;z++){
                    System.out.println("Dimension "+i+"-"+t+" : "+ints[i][t][z]);
                }
            }
        }
    }

    /* Result
        Dimension 0-0 : 1
        Dimension 0-0 : 2
        Dimension 0-0 : 3
        Dimension 0-1 : 4
        Dimension 0-1 : 5
        Dimension 0-1 : 6
        Dimension 1-2 : 7   // skipped the empty dimentions 1-0,1-1
        Dimension 1-2 : 8
        Dimension 1-2 : 9
        */

但有一条建议,我认为以 -1 开始循环是造成这种情况的原因......请尝试以“0”开始你的 for 循环......你的代码试图找到索引 -1 的元素导致异常的数组

您可以捕获此异常并“吃掉/跳过”它,但这是个坏主意

于 2013-09-20T23:12:30.147 回答