在Java中围绕数组中的特定点添加值

时间:2017-01-01 12:01:08

标签: java arrays if-statement for-loop multidimensional-array

所以我有一个填充了1和0的2D数组。我想检查数组中特定索引的邻居,并添加它们的值。

第一个和最后一个行和列(也就是'边界'值)是特殊情况,因为它们没有被相邻值完全包围,这意味着我必须考虑很多条件来考虑它们。

如果我只做第一个if语句,我会遇到arrayIndexOutOfBounds的问题。例如,当我尝试定位integerGeneration [-1] [ - 1]时,这对我来说很有意义。

我在下面所做的工作,但它真的很难看,我觉得有一个“整洁”的方法。

有没有比在自己的if语句中使用数组外边框上的所有特殊情况更好的方法?

if ((x > 0 & x < rows-1) & (y > 0 & y < columns-1) ) {      // checks the inside box
    for (int i = x - 1; i < x + 2; i++) {
        for (int j = y - 1; j < y + 2; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (x == 0 & y < columns-1 & y > 0) {                  // checks the top edge
    for (int i = x; i < x + 2; i++) {
        for (int j = (y - 1); j < y + 2; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (y == 0 & x < rows-1 & x > 0) {                     // checks the left edge
    for (int i = x - 1; i < x + 2; i++) {
        for (int j = y; j < y + 2; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (x == 0 & y == 0) {                                 // checks the top left corner
    for (int i = x; i < x + 2; i++) {
        for (int j = y; j < y + 2; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (x == rows-1 & y < columns-1 & y > 0) {             // checks the bottom edge
    for (int i = x - 1; i < x + 1; i++) {
        for (int j = y - 1; j < y + 2; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (y == columns-1 & x < rows-1 & x > 0) {             // checks the right edge
    for (int i = x - 1; i < x + 2; i++) {
        for (int j = y - 1; j < y + 1; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (y == columns-1 & x == rows-1) {                    // checks the bottom right corner
    for (int i = x - 1; i < x + 1; i++) {
        for (int j = y - 1; j < y + 1; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (x == 0 & y == columns-1) {                         // checks the top right corner
    for (int i = x; i < x + 2; i++) {
        for (int j = y - 1; j < y + 1; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (x == rows-1 & y == 0) {                            // checks the bottom left corner
    for (int i = x - 1; i < x + 1; i++) {
        for (int j = y; j < y + 2; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else {
    System.out.println("Error, point out of bounds");
    return -1;
}

}

1 个答案:

答案 0 :(得分:1)

检查一下。

   filled=0;
for (int i = x - 1; i < x + 2; i++)
{
    for (int j = y - 1; j < y + 2; j++) 
    {
        if(i<0 || i>=rows || j<0 || j>=columns || i==x || j==y)
               continue;

        filled = integerGeneration[i][j] + filled;
    }
}
return filled;