【发布时间】:2015-11-22 21:42:46
【问题描述】:
我即将完成一项作业,但我遇到了一个似乎可以调试的错误。我想这是因为我看它太久了,我需要一些新鲜的眼睛。我的编译器在第 111 行和第 63 行给了我一个ArrayIndexOutOfBoundsExpection。我不确定这个问题是否要在代码审查中重新定位,所以如果它应该请随意标记它。我正在研究使用递归进行迷宫遍历的经典提示。我认为我的逻辑是可靠的,在我摆脱错误之前我无法运行它。我明白错误是什么,但我的问题正在解决。有什么帮助,谢谢!
public class mazeTraversal
{
private static int xStart = 2;
private static int yStart = 0;
private final static int TRIED = 0;
private final static char PATH = 'X';
private static boolean mazeStart = false;
private static int printCount = 0;
private static boolean solved = false;
private static char[][] maze = {
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
{ 'S', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' },
{ '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', 'F' },
{ '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }};
public static void printMaze()
{
int rows = 12;
int columns = 12;
//maze = new char[rows][columns];
for(int i = 0; i<rows; i++)
{
for(int j = 0; j<columns; j++)
{
System.out.print(maze[i][j]);
printCount ++;
}
System.out.println();
}
}
public static boolean getSolved()
{
return solved;
}
public static boolean Traversal (char[][] maze, int row, int col)
{
//if-else to see if this is the first time the maze has been printed. Ie, is this the beginning?
if(printCount == 1)
{
mazeStart = true;
row = xStart;
col = yStart;
}
else
{
mazeStart = false;
}
boolean done = false;
boolean result = valid(row,col); //ERROR THROWN HERE
if( result == false)
{
maze[row][col] = TRIED; //Tried = 0
}
if(result == true)
{
//flag that maze is solved
if(row == maze.length - 1 && col == maze[0].length - 1)
{
done = true; //maze is solved
solved = true;
}
else
{
done = Traversal(maze, row+1, col); //down
if (!done)
{
done = Traversal(maze, row, col+1); //right
}
if (!done)
{
done = Traversal(maze, row-1, col); //up
}
if (!done)
{
done = Traversal(maze, row, col-1); //left
}
}
if (done)
{
maze[row][col] = PATH;
}
//return done;
}
return done;
}
//method to check if the space to move to next is vaild and not a wall
private static boolean valid(int row, int col)
{
boolean result = false;
if(maze[row][col] == '.') //ERROR THROWN HERE
{
result = true;
}
return result;
}
}
主要:
public class MazeTraversal_Hard
{
private static char[][] maze = {
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
{ 'S', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' },
{ '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', 'F' },
{ '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
{ '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }};
private static int row = 12;
private static int col = 12;
private static boolean solved = true;
public static void main(String[] args)
{
while(mazeTraversal.getSolved() != true)
{
mazeTraversal.printMaze();
mazeTraversal.Traversal(maze, row, col);
}
}
【问题讨论】:
-
你能标出抛出异常的行吗?
-
@SarathChandra 对不起,我忘记了,他们现在应该在行中发表评论。
-
数组长度为 12,您正在尝试访问第 12 个索引。但最高指数是 11。
标签: java recursion indexoutofboundsexception