So I have a 2D array which is filled with 1's and 0's. I want to check the neighbours of a specific index in the array, and add their values up.
The first and last rows and columns (aka the 'bordering' values) are special cases as they are not completely surrounded with neighbouring values which means I have to put lots of conditionals to take them into account.
If I only do the first if statement, I get the problem of arrayIndexOutOfBounds. Which makes sense to me as its trying to go to position integerGeneration[-1][-1], for example.
What I have done below works, but its really ugly and I feel there is a "neater" approach to this.
Is there a better way than doing all the special cases on the outer borders of the array in their own else if statements?
// 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;
}
Check this out:
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;
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加