繁体   English   中英

填充2D阵列迷宫时,阵列超出边界异常

[英]Array out of Bounds Exception when populating 2D Array maze

我目前正准备参加“数据结构”课程,因此,事先已经开始阅读某些主题。 我目前正在研究Stacks,但是遇到了问题。

我目前正在编写一个使用堆栈自动解决迷宫的Maze App。 但是,当它进入迷宫本身时,我遇到了一个问题。

代码如下:

import java.io.*;
import java.util.*;


public class Maze {
    private Square move;
    private char[][] maze;
    private SquareStack s;
    private String path = 
                 "C:\\Users\\Sigh\\workspace\\StegmannStackMaze\\maze.txt";
    private File file = new File(path);

public Maze(){
    s = new SquareStack();
    maze = new char[12][12];

}

public void getMaze() throws IOException{
    for (int row = 0; row < 12 ; ++row){ // Creates the left/right walls of the maze " |  | "
        maze[row][0] = '1';
        maze[row][11] = '1';
    }

    for ( int col = 0; col < 12 ; ++col){ // Creates upper and lower walls of the maze 
        maze[0][col] = '1';
        maze[11][col] = '1';
        }


Scanner filescan = new Scanner(path);
for( int row = 1; row <= 10 ; ++row){
    String line = filescan.nextLine();
    String delim = "[ ]+";
    String[] tokens = line.split(delim);

    for(int col = 1; col <= 10; ++col)
        maze[row][col] = tokens[col-1].charAt(0);
        }
filescan.close();
    }

}

这是.txt文件

0 0 1 E 1 0 0 1 1 1
0 1 1 0 1 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0
1 1 1 1 1 0 1 1 0 0
0 0 0 1 0 0 0 1 0 1
0 1 0 1 0 1 1 1 0 1
0 1 0 1 0 0 0 1 0 0
1 1 0 1 1 1 0 1 1 0
0 1 0 0 0 0 0 1 1 0 
0 1 0 1 1 0 1 0 0 0

一旦col = 2,异常本身就会在此特定行上发生。

for(int col = 1; col <= 10; ++col)
    maze[row][col] = tokens[col-1].charAt(0);
    }

根据我的收集,此行将使用创建的每个标记并填充该列。 但是,我不确定为什么会例外。

感谢您的阅读,希望我能从你们那里获得一些见识。

变化

   Scanner filescan = new Scanner(path);

    Scanner filescan = new Scanner(file);

该路径是一个String变量,而不是File实例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM