gpt4 book ai didi

java - 解决简单的 ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-02 04:10:20 25 4
gpt4 key购买 nike

我即将完成一项作业,但出现了一个似乎可以调试的错误。我想这是因为我已经看它太久了,我需要一些新的眼光。我的编译器为我提供了第 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);
}


}

最佳答案

在作为静态数组引入的维度为 [12][12] 的数组中,最大索引为 11。

private static int row = 12;
private static int col = 12;

你访问12,12索引,但是没有。这两行应该是

private static int row = 11;
private static int col = 11;

关于java - 解决简单的 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33860593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com