Java ArrayIndexOutOfBounds深度优先搜索
我当前在角色数组中遇到ArrayIndexOutOfBounds异常。我想我有很多问题。这个程序应该使用我创建的堆栈类解决一个迷宫。以下是我的错误: 1.Java ArrayIndexOutOfBounds深度优先搜索,java,stack,depth-first-search,Java,Stack,Depth First Search,我当前在角色数组中遇到ArrayIndexOutOfBounds异常。我想我有很多问题。这个程序应该使用我创建的堆栈类解决一个迷宫。以下是我的错误: 1.LinkedStackclass打印“堆栈为空”和“无法删除任何内容”,我不确定这是否是由索引错误或其他原因造成的 2.IndexOutOfBoundsException,我认为这是由于一个小的逻辑错误造成的 我目前正在使用其他人的算法,只是因为我以前做过,但方式不同。我不太确定是什么导致了这些错误,我猜我正在尝试访问迷宫之外的位置。异常发生在
LinkedStack
class打印“堆栈为空”和“无法删除任何内容”,我不确定这是否是由索引错误或其他原因造成的
2.IndexOutOfBoundsException
,我认为这是由于一个小的逻辑错误造成的
我目前正在使用其他人的算法,只是因为我以前做过,但方式不同。我不太确定是什么导致了这些错误,我猜我正在尝试访问迷宫之外的位置。异常发生在传递maze的行中,IndexOutOfBounds
的值为-1
样本迷宫(s=开始,f=结束,*=墙)
我的LinkedStack类
import java.awt.Point;
public class LinkedStack {
private Node top;
public LinkedStack() {
top = null;
}
public boolean isEmpty() {
return top == null;
}
public void push( Point p ) {
top = new Node (p, top);
}
public Point pop() {
Point retVal = new Point(0,0);
if( isEmpty() ){
System.out.println("Nothing to remove");
}else{
retVal = top.getValue();
top = top.getNext();
}
return retVal;
}
public Point peek() {
Point retVal = new Point(0,0);
if( isEmpty() ){
System.out.println("Stack is Empty");
}else{
retVal = top.getValue();
}
return retVal;
}
public String toString(){
String s = "";
Node n = top;
while( n != null ){
s = s + n.getValue() + " ";
n = n.getNext();
}
return s;
}
}
更新的主索引、固定索引自动边界现在获得StackOverFlow
//Creates an empty stack and calls method to get starting point
public static void solveDFS( char [][] maze ){
LinkedStack stack = new LinkedStack();
Point start = findPoint( maze,'s' );
findPath( maze,start,stack );
}
//Finds the point of the start and finish by searching array
private static Point findPoint( char [][] maze, char c ) {
for ( int i = 0; i < maze.length; i++ ) {
for ( int j = 0; j < maze[i].length; j++ ) {
if ( maze[i][j] == c ) {
return new Point(i, j);
}
}
}
return null;
}
//Should mark location of path taken with '.'
//Should check neighboring spots of location (up,right,down,left)
//Should check if valid locations
public static boolean findPath( char [][] maze, Point location, LinkedStack stack ){
boolean hasSolution = false;
stack.push(location);
do{
maze[location.x][location.y] = '.';
if( location.y > 0 ){
if( maze[location.x][location.y - 1] == ' '){
stack.push(new Point( location.x, location.y - 1));
maze[location.x][location.y - 1] = '.';
}
}
if( location.y < maze[location.x].length ){
if( maze[location.x][location.y + 1] == ' '){
stack.push(new Point( location.x, location.y + 1));
maze[location.x][location.y + 1] = '.';
}
}
if( location.x < maze.length ){
if( maze[location.x + 1][location.y] == ' '){
stack.push(new Point( location.x + 1, location.y ));
maze[location.x + 1][location.y] = '.';
}
}
if( location.x > 0 ){
if( maze[location.x - 1][location.y] == ' '){
stack.push(new Point( location.x - 1, location.y ));
maze[location.x - 1][location.y] = '.';
}
}
if( maze[location.x][location.y] == 'f' ){
hasSolution = true;
}
location = stack.peek();
stack.pop();
findPath( maze,location,stack );
}while( !location.equals('f') && !stack.isEmpty() );
return hasSolution;
}
//创建一个空堆栈并调用方法以获取起点
公共静态void solveDFS(字符[][]迷宫){
LinkedStack堆栈=新LinkedStack();
点开始=查找点(迷宫,'s');
findPath(迷宫、启动、堆栈);
}
//通过搜索数组查找起点和终点
专用静态点findPoint(字符[][]迷宫,字符c){
对于(int i=0;i0){
如果(迷宫[location.x][location.y-1]=''){
堆栈推送(新点(位置.x,位置.y-1));
迷宫[location.x][location.y-1]=';
}
}
if(位置.y0){
如果(迷宫[location.x-1][location.y]=''){
推(新点(位置.x-1,位置.y));
迷宫[location.x-1][location.y]=';
}
}
如果(迷宫[location.x][location.y]=='f'){
hasSolution=真;
}
位置=stack.peek();
stack.pop();
findPath(迷宫、位置、堆栈);
}而(!location.equals('f')&&!stack.isEmpty());
返回解决方案;
}
无论何时尝试获取可能不存在的索引,都应该进行边界检查
例如,maze[location.x][location.y+1]
如果y+1>maze[location.x].length,将抛出IndexOutOfBoundsException
更新:堆栈溢出异常
我认为这是因为您总是在计算while循环的条件之前执行递归findPath
调用。这意味着您永远不会退出do while循环
您需要添加一个终止递归调用的条件 (注意:我假设迷宫的左上角是迷宫[0][0],X是水平的,Y是垂直的。)
您的核心问题不是在以下几点检查是否处于迷宫边缘:
if( maze[location.x][location.y - 1] == ' '){
stack.push(new Point( location.x, location.y - 1));
maze[location.x][location.y - 1] = '.';
}
^如果位置
位于迷宫顶部边缘的任何位置,则此操作将失败
if( maze[location.x][location.y + 1] == ' '){
stack.push(new Point( location.x, location.y + 1));
maze[location.x][location.y + 1] = '.';
}
if( maze[location.x + 1][location.y] == ' '){
stack.push(new Point( location.x + 1, location.y ));
maze[location.x + 1][location.y] = '.';
}
if( maze[location.x - 1][location.y] == ' '){
stack.push(new Point( location.x - 1, location.y ));
maze[location.x - 1][location.y] = '.';
}
^如果位置
位于迷宫底部边缘的任何位置,则此操作将失败
if( maze[location.x][location.y + 1] == ' '){
stack.push(new Point( location.x, location.y + 1));
maze[location.x][location.y + 1] = '.';
}
if( maze[location.x + 1][location.y] == ' '){
stack.push(new Point( location.x + 1, location.y ));
maze[location.x + 1][location.y] = '.';
}
if( maze[location.x - 1][location.y] == ' '){
stack.push(new Point( location.x - 1, location.y ));
maze[location.x - 1][location.y] = '.';
}
^如果位置
位于迷宫右边缘的任何位置,则此操作将失败
if( maze[location.x][location.y + 1] == ' '){
stack.push(new Point( location.x, location.y + 1));
maze[location.x][location.y + 1] = '.';
}
if( maze[location.x + 1][location.y] == ' '){
stack.push(new Point( location.x + 1, location.y ));
maze[location.x + 1][location.y] = '.';
}
if( maze[location.x - 1][location.y] == ' '){
stack.push(new Point( location.x - 1, location.y ));
maze[location.x - 1][location.y] = '.';
}
^如果位置
位于迷宫左边缘的任何位置,则此操作将失败
if( maze[location.x][location.y + 1] == ' '){
stack.push(new Point( location.x, location.y + 1));
maze[location.x][location.y + 1] = '.';
}
if( maze[location.x + 1][location.y] == ' '){
stack.push(new Point( location.x + 1, location.y ));
maze[location.x + 1][location.y] = '.';
}
if( maze[location.x - 1][location.y] == ' '){
stack.push(new Point( location.x - 1, location.y ));
maze[location.x - 1][location.y] = '.';
}
因此,您可以将这4个检查打包为它们自己的检查:
if(location.y > 0) {
if( maze[location.x][location.y - 1] == ' '){
stack.push(new Point( location.x, location.y - 1));
maze[location.x][location.y - 1] = '.';
}
}
if(location.y < maze[location.x].length) {
if( maze[location.x][location.y + 1] == ' '){
stack.push(new Point( location.x, location.y + 1));
maze[location.x][location.y + 1] = '.';
}
}
if(location.x < maze.length) {
if( maze[location.x + 1][location.y] == ' '){
stack.push(new Point( location.x + 1, location.y ));
maze[location.x + 1][location.y] = '.';
}
}
if(location.x > 0) {
if( maze[location.x - 1][location.y] == ' '){
stack.push(new Point( location.x - 1, location.y ));
maze[location.x - 1][location.y] = '.';
}
}
if(location.y>0){
如果(迷宫[location.x][location.y-1]=''){
堆栈推送(新点(位置.x,位置.y-1));
迷宫[location.x][location.y-1]=';
}
}
if(位置.y0){
如果(迷宫[location.x-1][location.y]=''){
推(新点(位置.x-1,位置.y));
迷宫[location.x-1][location.y]=';
}
}
现在,如果逻辑上能够,他们只会检查相邻的磁贴。您将在语句maze[location.x-1][location.y]中获得IndexOutOfBoundsException
当location.x
将为0时。您需要在findPoint
方法中从1开始循环i和j的变量值,而不是从0开始,或者在findPath
方法中更改逻辑以访问array
method@Prashant我做了这些更改,但仍然得到相同的异常?同样适用于y
我做了这些更改,不再获得IndexOutOfBoundsException。但我现在遇到了一个新错误,即每当函数调用自身时,findPath()中就会发生StackOverFlow。我有upda