2

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;
}
7
  • That is a lot of code duplication. Why don't you just have the loops iterate over the "surrounding" elements and check the relevant index before trying to access (e.g.: if(x < 0) continue;) Commented Jan 1, 2017 at 12:05
  • Also this might be better suited for CodeReview Commented Jan 1, 2017 at 12:22
  • @UnholySheep Oh, oops. Do you mean something like this? public static int neighbours(int x, int y) { // get the number of filled neighbours int 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; else filled += integerGeneration[i][j]; } } return filled; } Commented Jan 1, 2017 at 12:59
  • oh wow, and im not allowed to edit that D: ` 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; else filled += integerGeneration[i][j]; } } return filled;` Commented Jan 1, 2017 at 13:03
  • diddnt help much xD Commented Jan 1, 2017 at 13:03

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

6 Comments

shouldn't it be i==x && j==y?
yeah sorry it was typo
no worries, was just checking to make sure I understood that correctly :P Thank you very much!
if(i<0 || i>=rows || j<0 || j>=columns || (i==x && j==y))
welcome and you can also upvote the answer if you are satisfied
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.