2017-01-01 1 views
1

Also habe ich ein 2D-Array, das mit 1 und 0 gefüllt ist. Ich möchte die Nachbarn eines bestimmten Index im Array überprüfen und ihre Werte hinzufügen.Addieren von Werten um einen bestimmten Punkt in einem Array in Java

Die ersten und letzten Zeilen und Spalten (auch die 'grenzenden' Werte) sind Sonderfälle, da sie nicht vollständig mit benachbarten Werten umgeben sind, was bedeutet, dass ich viele Bedingungen dafür angeben muss.

Wenn ich nur die erste if-Anweisung mache, bekomme ich das Problem von arrayIndexOutOfBounds. Das macht für mich Sinn, wenn es zum Beispiel in die Position ganzzahligeGeneration [-1] [- 1] geht.

Was ich unten getan habe funktioniert, aber es ist wirklich hässlich und ich fühle, dass es einen "sauberen" Ansatz dazu gibt.

Gibt es einen besseren Weg, als alle Sonderfälle auf den äußeren Grenzen des Arrays in ihren eigenen else if-Anweisungen zu tun?

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

}

+0

Das ist eine Menge Code-Duplizierung. Warum schleifen Sie nicht einfach die Schleifen über die "umgebenden" Elemente und überprüfen Sie den relevanten Index, bevor Sie versuchen, darauf zuzugreifen (zB: 'if (x <0) weiter;') – UnholySheep

+0

Auch dies ist möglicherweise besser geeignet für [CodeReview] (http://codereview.stackexchange.com) – UnholySheep

+0

@ UnholySheep Oh, oops. Meinst du sowas? \t public static int Nachbarn (int x, int y) {\t \t \t \t \t \t \t \t \t // die Anzahl der gefüllten Nachbarn \t \t int = 0 gefüllt bekommen; \t \t for (int i = x - 1; i = Zeilen) | (j < 0 | j > = Spalten) | (i == x & j == y)) \t \t \t \t \t weiter; \t \t \t \t sonst \t \t \t \t \t gefüllt + = integerGeneration [i] [j]; \t \t \t } \t \t } \t \t return gefüllt; \t} – Sev

Antwort

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; 
+0

sollte es nicht sein i == x && j == y? – Sev

+0

yeah Entschuldigung es war Tippfehler – skag

+0

keine Sorgen, war nur zu überprüfen, dass ich das richtig verstanden habe: P Vielen Dank! – Sev

Verwandte Themen