Henry Zhu
Henry Zhu

Reputation: 2618

Maze Solver ArrayIndexOutOfBoundsException

I am working on a Maze Solver, but I encounted an ArrayOutOfBoundsException on this line:

wasHere[row][col] = false;

I don't see how there could possibly be such error, since the width and height (number of rows and columns) as represented in the maze, wasHere, and correctPath are all the same. All arrays show 7 columns and 4 rows.

private static int[][] maze = {{2, 2, 2, 2, 1, 2, 2}, {2, 2, 2, 2, 1, 2, 2}, 
        {2, 2, 2, 2, 1, 2, 2}, {2, 1, 1, 1, 1, 2, 2}}; // The maze
private static boolean[][] wasHere = new boolean[7][4];
private static boolean[][] correctPath = new boolean[7][4]; // Solution

for (int row = 0; row < maze.length; row++)
{
    // Sets boolean arrays to false
    for (int col = 0; col < maze[row].length; col++)
    {
        wasHere[row][col] = false;
        correctPath[row][col] = false;
    }
}

Upvotes: 1

Views: 68

Answers (1)

James
James

Reputation: 1238

You defined wasHere to be 7 by 4, whereas maze is 4 by 7. You will get an error when you try to access, for example, row = 0, col = 4.

Upvotes: 2

Related Questions