그래서 1과 0으로 채워진 2D 배열이 있습니다. 배열의 특정 인덱스의 이웃을 확인하고 값을 위로 추가하려고합니다.Java 배열의 특정 지점 주위에 값을 더함
첫 번째와 마지막 행과 열 ('경계 값'이라고도 함)은 이웃 값으로 완전히 둘러싸여 있지 않으므로 특별한 경우입니다. 즉, 많은 조건을 고려해야한다는 의미입니다.
첫 번째 if 문만 수행하면 arrayIndexOutOfBounds 문제가 발생합니다. 예를 들어, integerGeneration [-1] [- 1]의 위치를 지정하려고 시도하는 것이 저에게 의미가 있습니다.
나는 아래의 작업을했지만 실제로는보기 흉한 느낌이 들었습니다.
다른 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 공공 정적 이웃 INT (INT의 X, Y의 INT) {\t \t \t \t \t \t \t \t \t \t \t // INT = 0 가득 채워진 이웃의 수를 얻는다; 대 { \t \t \t (INT의 J = Y - 1; J = 로우) | (j = < 0 | j > 열) | (I == X 및 J에 ==의 Y)) \t \t \t \t \t 계속; 다른 \t \t \t \t \t \t \t \t \t 가득 integerGeneration + = [I] [J] \t \t \t} \t \t} 작성 \t \t 반환; \t} –
Sev