【问题标题】:Adding up values around a specific point in an array in Java在Java中将数组中特定点周围的值相加
【发布时间】:2017-05-15 21:06:06
【问题描述】:

所以我有一个用 1 和 0 填充的二维数组。我想检查数组中特定索引的邻居,并将它们的值相加。

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

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

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

有没有比在它们自己的 else if 语句中处理数组外边界上的所有特殊情况更好的方法?

// checks the inside box
if ((x > 0 & x < rows - 1) & (y > 0 & y < columns - 1)) {
    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;
// checks the top edge
} else if (x == 0 & y < columns - 1 & y > 0) {
    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;
// checks the left edge
} else if (y == 0 & x < rows - 1 & x > 0) {
    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;
// checks the top left corner
} else if (x == 0 & y == 0) {
    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;
// checks the bottom edge
} else if (x == rows - 1 & y < columns - 1 & y > 0) {
    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;
// checks the right edge
} else if (y == columns - 1 & x < rows - 1 & x > 0) {
    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;
// checks the bottom right corner
} else if (y == columns - 1 & x == rows - 1) {
    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;
// checks the top right corner
} else if (x == 0 & y == columns - 1) {
    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;
// checks the bottom left corner
} else if (x == rows - 1 & y == 0) {
    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;
}

【问题讨论】:

  • 那是很多代码重复。为什么不让循环遍历“周围”元素并在尝试访问之前检查相关索引(例如:if(x &lt; 0) continue;
  • 这可能更适合CodeReview
  • @UnholySheep 哦,哎呀。你的意思是这样的吗? public static int neighbours(int x, int y) { // 获取填充邻居的数量 intfilled = 0; for (int i = x - 1; i = rows) | ( j = 列) | (i == x & j == y)) 继续;否则填充 += integerGeneration[i][j]; } } 返回填充; }
  • 哇,我不允许编辑那个 D: ` for (int i = x - 1; i = rows) | (j = columns) | (i == x & j == y)) 继续;否则填充 += integerGeneration[i][j]; } } 返回填充;`
  • 对xD帮助不大

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


【解决方案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;

【讨论】:

  • 不应该是i==x && j==y吗?
  • 是的,很抱歉是错字
  • 别担心,只是检查以确保我理解正确:P 非常感谢!
  • if(i=rows || j=columns || (i==x && j==y))
  • 欢迎您,如果您满意,也可以为答案投票
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-18
  • 1970-01-01
相关资源
最近更新 更多