【问题标题】:Solving a simple ArrayIndexOutOfBoundsException解决一个简单的 ArrayIndexOutOfBoundsException
【发布时间】: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


【解决方案1】:

在您作为静态数组引入的维度为 [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;

【讨论】:

  • 我已经想过并尝试过了,但问题仍然存在,但感谢您的回复。
  • 我是个白痴,我想是时候休息一下了。那确实解决了我的问题。谢谢。
  • Nice ;) - 但是现在你正面临一个无限循环。看来你还有一些工作要做。
猜你喜欢
  • 1970-01-01
  • 2016-07-28
  • 2010-12-28
  • 1970-01-01
  • 2012-06-20
  • 2020-04-03
  • 2012-03-22
  • 2014-11-19
  • 2021-02-04
相关资源
最近更新 更多