2D 배열 (Cell[][]
격자)을 만들고 각 셀에 neighborCells
이라는 ArrayList
; 그리고 지금 나는 그 이웃 세포를 찾으려고 노력하고있다. 그러나 나는 IndexOutOfBoundsException
를 얻고있다. 도울 수 있니?2D 배열에서 이웃 셀을 찾고 IndexOutOfBounds 가져 오기
ArrayList<Cell> neighborCells = new ArrayList();
for(int i = 0; i < grid.length; i++){
for(int j = 0; j < grid.length; j++) { //we can also use grid.length since it is the same
int startPositionX = (i - 1 < 0) ? i : i - 1;
int startPositionY = (j - 1 < 0) ? j : j - 1;
int endPositionX = (i + 1 > grid.length) ? i : i + 1;
int endPositionY = (j + 1 > grid.length) ? j : j + 1;
for (int row = startPositionX; row <= endPositionX; row++) {
for (int col = startPositionY; col <= endPositionY; col++) {
neighborCells.add(grid[row][col]); // here is the error
}
}
}
}
ArrayList 정의 방법은 무엇입니까? .NET 클래스 또는 자바 클래스입니까? – Wolf
자바 클래스 – flower
네, 답변을 망쳐 버려서 죄송합니다. 너는 그것을 다시 편집했다. 나는 지금 그것이 정확하지 않다고 생각한다. 'neighborCells'는 당신이 얻고 자하는 이웃들의 모임이 아닌가요? – Wolf