所以我有一個填充了1和0的二維數組。我想檢查數組中某個特定索引的鄰居,並將它們的值加起來。在Java中的數組中的特定點周圍添加值
第一個也是最後一個行和列(也稱爲「邊界」值)是特殊情況,因爲它們沒有被相鄰值完全包圍,這意味着我必須將很多條件考慮在內。
如果我只做第一個if語句,我得到arrayIndexOutOfBounds的問題。例如,它對我來說有意義,因爲它試圖定位到integerGeneration [-1] [ - 1]。
我在下面的工作做了什麼,但它的確很醜,我覺得這裏有一個「整潔」的方法。
有沒有更好的辦法比在數組的外部邊界上做所有的特殊情況在他們自己的其他語句?
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;
}
}
這是很多代碼重複。爲什麼不只是循環遍歷「surround」元素並在嘗試訪問前檢查相關索引(例如:if(x <0)continue;') – UnholySheep
此外,這可能更適合於[CodeReview] (http://codereview.stackexchange.com) – UnholySheep
@UnholySheep哦,哎呀。你的意思是這樣嗎? \t公共靜態INT鄰居(INT的x,int y)對{\t \t \t \t \t \t \t \t \t //獲得填充鄰居 \t \t INT填充= 0的數目; \t \t對(INT I = X - 1;我 =行)|(j < 0 | j > =列)|(I == X&J == y)的) \t \t \t \t \t繼續; \t \t \t \t別的 \t \t \t \t \t填充+ = integerGeneration [i] [j]; \t \t \t} \t \t} \t \t回報填補; \t} –
Sev