【问题标题】:ArrayIndexOutOfBounds Java(Now stack overflow error!?)ArrayIndexOutOfBounds Java(现在堆栈溢出错误!?)
【发布时间】:2014-04-10 00:33:39
【问题描述】:

我正在尝试编写一个程序,该程序返回是/否,无论迷宫是否可遍历,但不断收到此异常...怎么了?我怎样才能阻止这种情况发生? (我想尽可能保持这种结构)

迷宫:

WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW
WSOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOOOWOOOOOOOOOOOOOOOWOOOOOOW
WWOOOOOOOOOOOOOWWWWWWWWWWWWWOOOOOOOOOOWWWWWWWWWWWWWOOOOOOW
WWWWWWOOOOOOOOOOOOWWWWWWWOOOOOOOOOOOOWWWWWWWWWWWWWWWWOOOOW
WOOOOOOWWWWWWWWWWWWWWOOOOOOOOOOOWWWWWWWWOOOOOOOOOOOOOOOWWW
WOOOOWWWWWWWOOOOOOWWWWOOOOOOWWWWWWWWWWWOOOOWWWWWWWWWOWWWWW
WOOOWWWWWWWWWWWWOOWWWWWWWWWWWWOOOOOOOOOOOOWWWWWWWWWOOOOOWW
WOOWWWWWWWWWWWWWOOWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWWOOOW
WOWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWOOOOOOOWWWWWWWWWWWOOW
WOWWWWWWWWWWWWWOOOOOOOOOOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWOOW
WOOOOOOOOOOOOOOOOWWWWOOOOOOOOWWWWWWWOOOOOOWWWWWWWWWWWWWWFW
WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW

新代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Stack;
import java.awt.Point;

public class MazeExplorer {
    static Point startPoint = new Point();
    static Point finishPoint = new Point();
    final static int mazeHeight = 12;
    final static int mazeWidth = 58;
    static char[][] mazePoints = new char[mazeHeight][mazeWidth];
    Stack<Point> pointsNotTraversed = new Stack<Point>();
    Point pt = new Point();
    static HashSet<Point> previousLocations = new HashSet<Point>();
    static Stack<Point> nextPoints = new Stack<Point>();

    public static void main(String[] args) throws FileNotFoundException{

        System.out.println("Please enter the file name of your Maze");
        Scanner console = new Scanner(System.in);
        File f = new File(console.nextLine());
        Scanner sc = new Scanner(f);

        if(!sc.hasNextLine()){
            System.out.println("Sorry, please enter a file name with the extension, that contains a maze!");
        }
        System.out.println("So, you want to know if your maze is solvable.....?");

        for (int row = 0; row < mazeHeight && sc.hasNext(); row++) {
            final String mazeRow = sc.next(); //Get the next row from the scanner.
            mazePoints[row] = mazeRow.toCharArray(); //Convert the row into a char[].
        }
            //identify the finish point
        for(int i = 0; i < mazeHeight; i++){
            for(int j = 0; j<mazeWidth; j++){
                if(mazePoints[i][j] == 'F'){
                    finishPoint = new Point(i, j);
                }       
            }
        }
        // Identify the start point
       for(int i = 0; i< mazeHeight; i++){
           for(int j = 0; j < mazeWidth; j++){
               if(mazePoints[i][j] == 'S'){
                 startPoint = new Point(i , j);
               }
           }
       }
       isTraversable(startPoint);    
    }
        public static  boolean isTraversable(Point current){
            boolean isSolvable = false;
            nextPoints.push(current);

            do {


                if(current.y < 11) {
                    if((mazePoints[current.y + 1][current.x] != ' ') && (mazePoints[current.y + 1][current.x] != 'W') ){ // below direction
                    nextPoints.push(new Point(current.y + 1, current.x));
                    mazePoints[current.y + 1][current.x] = ' ';        
                    isTraversable(nextPoints.pop());    

                }
                }
                if(current.y > 0){


                if (mazePoints[current.y - 1][current.x] != ' ' && mazePoints[current.y - 1][current.x] != 'W' ){ //up dir
                   nextPoints.push(new Point(current.y - 1, current.x));
                    mazePoints[current.y - 1][current.x] = ' ';  //'X' marks where you've already been
                   isTraversable(nextPoints.pop());     

                }
                }
                if(current.x < 57){
                if(mazePoints[current.y][current.x + 1] != ' ' && mazePoints[current.y][current.x + 1] != 'W'){ // to the right
                    nextPoints.push(new Point(current.y, current.x + 1));
                    mazePoints[current.y][current.x + 1] = ' ';
                    isTraversable(nextPoints.pop());    

                }
                }
                if(current.x > 0){


                if(mazePoints[current.y][current.x - 1] != ' ' && mazePoints[current.y][current.x - 1] != 'W') { // to the left
                    nextPoints.push(new Point(current.y, current.x - 1));
                    mazePoints[current.y][current.x - 1] = ' ';     
                    isTraversable(nextPoints.pop());    

                }
                }
                if(current.equals(finishPoint)){
                    isSolvable = true;
                    System.out.println("MAZE IS SOLVABLE, YAHOOOOOO!!!!");
                }




            } while(!current.equals('F') && !nextPoints.isEmpty());    


            return isSolvable;          
        }
}

【问题讨论】:

  • 哪个语句导致了错误?异常应该给你一行 #
  • 今天迷宫题太多了……
  • 你的堆栈跟踪是什么?
  • @David 我怎样才能找到?顺便说一句,这对我现在遇到的堆栈溢出错误有帮助吗?
  • @bazookyelmo 我更新了我的回复。道歉。我省略了 if/else。

标签: java arrays exception stack


【解决方案1】:
 if(current.x < 58){
                if(mazePoints[current.y][current.x + 1] == 'O'){ // to the right
                    nextPoints.push(new Point(current.y, current.x + 1));
                    mazePoints[current.y][current.x + 1] = ' ';
                }
                }

当 current.x = 57 时,您会得到数组索引绑定异常,将 58 替换为 57,因为您执行 current.x + 1 并且 58 indx 不存在

【讨论】:

  • 谢谢,改成57,其他问题也一样...现在我有堆栈溢出错误??
  • isTraversable 调用自身,这会导致堆栈溢出异常您可以先尝试使用小于 58 的数字来测试您的功能,然后将 isTraversable 函数从递归函数更改为迭代函数
  • 我怎样才能迭代地做到这一点..? @PulkitSethi?
  • 无法为你写完整的解决方案在线查算法示例
  • 我实际上已经解决了堆栈溢出问题......但是现在我的程序无限运行......?见编辑..
【解决方案2】:

发生这种情况是因为您的递归方法没有得到正确考虑。我冒昧地清理了一些方法,以便您可以看到它应该是什么样子。

我知道递归可能是一个棘手的话题,但如果你打算有效地学习,你会想要理解代码或更多地练习递归。

public static  boolean isTraversable(Point current){
        boolean isSolvable = false;
        mazePoints[current.x][current.y] = ' ';

        if(current.y < 57 && current.y > 0 && current.x > 0 && current.x < 12){
            if (mazePoints[current.x - 1][current.y] == 'O'){ // Up dir
                Point upPoint = new Point(current.x-1, current.y);
                nextPoints.push(upPoint);
            }

            if(mazePoints[current.x+1][current.y] == 'O'){ // Down dir
                Point downPoint = new Point(current.x+1, current.y);
                nextPoints.push(downPoint);
            }

            if(mazePoints[current.x][current.y + 1] == 'O'){ // to the right
                Point rightPoint = new Point(current.x, current.y+1);
                nextPoints.push(rightPoint);
            }

            if(mazePoints[current.x][current.y - 1] == 'O'){ // to the left
                Point leftPoint = new Point(current.x, current.y-1);
                nextPoints.push(leftPoint);
            }

            if(mazePoints[current.x - 1][current.y] == 'F' ||
               mazePoints[current.x + 1][current.y] == 'F' ||
               mazePoints[current.x][current.y - 1] == 'F' ||
               mazePoints[current.x][current.y + 1] == 'F'){
                System.out.println("MAZE IS SOLVABLE, YAHOOOOOO!!!!");
                return true;
            }

        }

        if(nextPoints.isEmpty()){
            return false;
        }
        else{
            current = nextPoints.pop();
        }

        return(isTraversable(current));

    }

【讨论】:

  • @bazookyelmo 我在最后留下了一个 else 语句。应该这样做。
  • 不,不是这样...我的迷宫文件有误! facepalm 它与您举例说明的迷宫完美搭配..(这是正确的迷宫)
  • @bazookyelmo 很高兴我能帮上忙。你有 13 个问题并且只接受了 3 个问题的答案。如果他们没有很好的答案,那很好,但如果没有标记有帮助的答案,请尽量不要放弃问题。祝你好运=]
  • 我马上就去!我真的更习惯于论坛,所以这是新的。感谢一切。关于 isTraversable 方法的一个问题......假设迷宫看起来像我上面发布的那个,那怎么解决?因为它需要回溯 F.. 上方的 0 的路径?
  • @bazookyelmo 哎呀。 if 语句差了一个。编辑修复
猜你喜欢
  • 2011-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 1970-01-01
  • 2014-01-26
相关资源
最近更新 更多