我正在开发迷宫求解器,但我在这一行遇到了 ArrayOutOfBoundsException:
wasHere[row][col] = false;
我不明白怎么可能出现这样的错误,因为迷宫
中表示的宽度和高度(行数和列数),wasHere
,和 CorrectPath
都是相同的。所有数组均显示7 列和4 行。
private static int[][] maze = {{2, 2, 2, 2, 1, 2, 2}, {2, 2, 2, 2, 1, 2, 2},
{2, 2, 2, 2, 1, 2, 2}, {2, 1, 1, 1, 1, 2, 2}}; // The maze
private static boolean[][] wasHere = new boolean[7][4];
private static boolean[][] correctPath = new boolean[7][4]; // Solution
for (int row = 0; row < maze.length; row++)
{
// Sets boolean arrays to false
for (int col = 0; col < maze[row].length; col++)
{
wasHere[row][col] = false;
correctPath[row][col] = false;
}
}
最佳答案
您将 wasHere
定义为 7 x 4,而 maze
为 4 x 7。当您尝试访问时,您会收到错误,例如 行 = 0
,列 = 4
。
关于java - 迷宫求解器 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31096365/