私は2次元配列として迷路を作成するプログラムを作成しています。私はちょっとした問題にぶつかって、ArrayIndexOutOfBoundsExceptionです。 drawMazeメソッドのmaze[0][0] = "S"
を指しています。私はこれで私の頭を傷つけている、私はなぜそれがエラーを投げているのか分からない。不明な理由によるArrayIndexOutOfBoundsException
import java.util.Random;
public class LA2_MazeSolver {
private int rows;
private int cols;
private String[][] maze = new String[rows][cols];
LA2_MazeInput mi = new LA2_MazeInput();
public void setNumRows(int numRows) {
this.rows = numRows;
}
public void setNumCols(int numCols) {
this.cols = numCols;
}
public int getNumRows() {
return this.rows;
}
public int getNumCols() {
return this.cols;
}
public void drawMaze() {
Random r = new Random();
maze[0][0] = "S";
maze[rows - 1][cols - 1] = "D";
int limit = ((rows * cols)/3);
for (int i = r.nextInt(limit) + 1; i < limit; i++) {
maze[r.nextInt(rows) - 1][r.nextInt(cols) - 1] = "#";
}
for (int i = 0; i < maze.length; i++) {
for (int c = 0; c < maze[0].length; c++) {
if (!(maze[i][c].matches("#")) && !(maze[i][c].matches("S")) && !(maze[i][c].matches("D"))) {
maze[i][c] = Integer.toString(r.nextInt(100) + 1);
}
}
}
}
public void printMaze() {
}
/*public boolean isSolvable() {
return solveMazeRecursively(this.rows, this.cols);
}
private boolean solveMazeRecursively(int row, int col) {
}*/
public void printResult() {
}
}
'より大きい値にrowsとcolsを初期化し、この問題を解決するには0
ですまだ設定されていない。 'maze'は' rows'と 'cols'が設定された時にのみ初期化する必要があります。 – 4castle
値を割り当てる前に、プログラムの上部またはコンストラクタで迷路を初期化する必要があります。行と列を特定のintに変更します。 – Waffles