したがって、1と0で埋められた2次元配列があります。私は、配列の特定のインデックスの隣人をチェックし、それらの値を追加します。Javaの配列内の特定の点の周りに値を加算する
最初と最後の行と列(別名 '境界値')は、隣接する値に完全に囲まれていない特殊なケースです。つまり、それらを考慮に入れるために多くの条件を付けなければなりません。
最初のif文だけを実行すると、arrayIndexOutOfBoundsの問題が発生します。これは、例えば、integerGeneration [-1] [ - 1]の位置に向かう試みとして私には意味があります。
私は以下の作業を行っていますが、実際には醜いですが、私はそこに「もっときれいな」アプローチがあると感じています。
独自のelse if文で配列の外側の境界にあるすべての特殊なケースを行うよりも良い方法はありますか?
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;
}
}
これはコードの重複の多くです。どうしても、 "周囲の"要素を反復して、アクセスしようとする前に関連するインデックスをチェックしないでください(例えば、 'if(x <0)continue;') – UnholySheep
またこれは[CodeReview] (http://codereview.stackexchange.com) – UnholySheep
@UnholySheepああ、おっと。このようなことを意味しますか? \tパブリックstatic int型ネイバー(int型のx、int型のY){\t \t \t \t \t \t \t \t \t // \t \t INT = 0を満たさ充填隣人の数を取得します。 { \t \t \tための - (;私は、x + 2 Sev