2

私はJavaでプログラムをテストしようとしていますが、スローされるべきだとは思わない例外の範囲外の配列インデックスを取得しています。このコードを見て、何かが足りないかどうか教えてください。eclipseは、コメントを追加した場所にエラーがスローされていることを示しています。

class maze{

private int cols; // number of columns in maze  
private int rows; // number of rows in maze
private String name;
private weightedGraph<Integer> graph;
private dijkstra solution;
public char[][] mazeStore;

public maze(String filename){

    try{

        FileReader r = new FileReader(filename);
        Scanner s = new Scanner(r);
        this.rows = s.nextInt();
        this.cols = s.nextInt();
        this.name = filename;


        this.mazeStore = new char[(2*rows)+1][(2*cols)+1];
        String line = s.nextLine();
        for(int k = 0; k < ((2*rows)+1); k++){

            char[] temp = line.toCharArray();

            for(int i = 0; i < temp.length; i++){
                mazeStore[k][i] = temp[i];
                line = s.nextLine();
            }
        }



        graph = new weightedGraph<Integer>(rows*cols); 


        for(int y = 1; y < 2*rows; y++){
            for(int x = 1; x < 2*cols; x++){
                if((x % 2 == 1) && (y % 2 == 0)){
                    if(mazeStore[x][y] != '-'){ // <<<<<<<<<<<<<<THIS IS WHERE THE ERROR IS THROWN 
                        int label = (x - 1) + (x / 2);
                        graph.addEdge(label, label+cols, 1);
                        graph.addEdge(label+cols, label, 1);
                    }
                }

                if((x % 2 == 0) && (y % 2 == 1)){
                    if(mazeStore[x][y] != '|'){
                        int label = ((x - 1) + (x / 2)) + (y / 2);
                        graph.addEdge(label, label+1, 1);
                        graph.addEdge(label+1, label, 1);
                    }
                }
            }
        }



        this.solution = new dijkstra(graph, 0); 


    }
    catch(FileNotFoundException e){
        System.err.println("FileNotFoundException: " + e.getMessage());
    }
4

2 に答える 2

5

アレイを初期化しました

new char[(2*rows)+1][(2*cols)+1] 

しかしそれを繰り返す

for(int y = 1; y < 2*rows; y++){//y row iterator
    for(int x = 1; x < 2*cols; x++){//x col iterator

だからそれはすべきではあり mazeStore[y][x]ませんmazeStore[x][y]

于 2012-05-31T17:12:47.420 に答える
2

変数が故障しています。youre outterほとんどのループは行に基づいていますが、列のサイズになるように初期化した配列でyoureを使用しています

于 2012-05-31T17:11:56.147 に答える