Adding up values around a specific point in an array in Java

Sev

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;
}
skag

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]

編集
0

コメントを追加

0

関連記事

分類Dev

Adding up values from array elements in MongoDB

分類Dev

Values in array don't adding

分類Dev

foreach loop adding string to specific values

分類Dev

Adding event listeners on array of pics in specific way

分類Dev

MongoDB Replace specific array values

分類Dev

Remove specific values from an array

分類Dev

jQuery - Split up list in two lists at a specific break point

分類Dev

how to remove points in an array around NaN values within a window

分類Dev

React Event Handler. Adding values to array

分類Dev

adding values in a 2d associative array

分類Dev

MongoDB aggregate - average on specific values in array of documents

分類Dev

Set specific values to zero in numpy array

分類Dev

Return specific values from array of hashes - JSON

分類Dev

Null point exception when looking up a Map in java.

分類Dev

How to remove array items between specific values that contains specific phrase

分類Dev

Appending session values to an Array in Java

分類Dev

Java array values being put into wrong array

分類Dev

Rotating around a point, object consistently getting further

分類Dev

Adding two lists point by point in haskell

分類Dev

Adding Parentheses around output in xsl report

分類Dev

jsonlite toJSON adding excess quotes around an object

分類Dev

Trouble with adding up cells in second column if the first ones are identical in double dimensional array

分類Dev

How to add specific values from array list to the Hash map?

分類Dev

Delete specific values in 2-Dimension array - Numpy

分類Dev

How to parse json to get all values of a specific key within an array?

分類Dev

A method to remove duplicates from a multi dimensional array by comparing specific values

分類Dev

JavaScript sort array of objects on multiple properties by specific constant values

分類Dev

Replacing values with for-loop in array in Java

分類Dev

How to get the values of generic array in Java?

Related 関連記事

  1. 1

    Adding up values from array elements in MongoDB

  2. 2

    Values in array don't adding

  3. 3

    foreach loop adding string to specific values

  4. 4

    Adding event listeners on array of pics in specific way

  5. 5

    MongoDB Replace specific array values

  6. 6

    Remove specific values from an array

  7. 7

    jQuery - Split up list in two lists at a specific break point

  8. 8

    how to remove points in an array around NaN values within a window

  9. 9

    React Event Handler. Adding values to array

  10. 10

    adding values in a 2d associative array

  11. 11

    MongoDB aggregate - average on specific values in array of documents

  12. 12

    Set specific values to zero in numpy array

  13. 13

    Return specific values from array of hashes - JSON

  14. 14

    Null point exception when looking up a Map in java.

  15. 15

    How to remove array items between specific values that contains specific phrase

  16. 16

    Appending session values to an Array in Java

  17. 17

    Java array values being put into wrong array

  18. 18

    Rotating around a point, object consistently getting further

  19. 19

    Adding two lists point by point in haskell

  20. 20

    Adding Parentheses around output in xsl report

  21. 21

    jsonlite toJSON adding excess quotes around an object

  22. 22

    Trouble with adding up cells in second column if the first ones are identical in double dimensional array

  23. 23

    How to add specific values from array list to the Hash map?

  24. 24

    Delete specific values in 2-Dimension array - Numpy

  25. 25

    How to parse json to get all values of a specific key within an array?

  26. 26

    A method to remove duplicates from a multi dimensional array by comparing specific values

  27. 27

    JavaScript sort array of objects on multiple properties by specific constant values

  28. 28

    Replacing values with for-loop in array in Java

  29. 29

    How to get the values of generic array in Java?

ホットタグ

アーカイブ