IndexOutofBoundsException:我无法解决它

时间:2015-06-21 15:17:18

标签: java indexoutofboundsexception

我试图做一个基本的java任务,有船只,我必须使用导入文件给出它们的坐标,其中有一些字符代表船只的运动。 (N = +1到北,S = +1到南,E = +1到东,W = +到西)

我想计算二维数组中的坐标,其中第一列代表垂直,第二列代表水平方向。

得到以下问题:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at robotok.main(robotok.java:36)

我在源代码中标记了第36行。

ArrayList<String> control = new ArrayList<String>();
int coords[][] = new int[control.size()][2]; // index = robot sorszáma / [] Hosszúság / [] Szélesség

try {
    Scanner scan = new Scanner(robotok);

    while (scan.hasNextLine()) {
        String line = scan.nextLine();
        control.add(line);
    }
} catch(Exception e){
}

for (int i = 0;i<control.size();i++) {
    char[] directions = control.get(i).toCharArray();

    for (int j =0;j<directions.length;j++) {
        if (directions[j] == 'N') {
            coords[i][0] --;
        }else if (directions[j] == 'S'){
            coords[i][0] ++;
        }else if (directions[j] == 'W'){
            coords[i][1] --;
        }else if (directions[j] == 'E'){
            coords[i][1] ++;                     /////THIS IS 36///// 
        }
    }
}

for (int i =0;i<coords.length;i++) {
     System.out.print("The "+i+". ship's coords: "+coords[i][0]+" ; "+coords[i][1]);
}

3 个答案:

答案 0 :(得分:5)

在将元素添加到coords之前创建control数组,而它仍然是大小0.您需要延迟创建它,直到列表达到最终大小。

答案 1 :(得分:0)

当您创建控件ArrayList时,其初始长度为0,并且在访问其长度之前不会在其中插入任何元素 因此,坐标的数组长度也为零,并且您尝试访问超出其长度的阵列 因此,建议在将值分配给长度

之前将值插入到控件中

答案 2 :(得分:0)

而不是

ArrayList<String> control = new ArrayList<String>();
int coords[][] = new int[control.size()][2]; // index = robot sorszáma / [] Hosszúság / [] Szélesség

try {
    Scanner scan = new Scanner(robotok);

    while (scan.hasNextLine()) {
        String line = scan.nextLine();
        control.add(line);
    }
} catch(Exception e){
}

for (int i = 0;i<control.size();i++) {
    char[] directions = control.get(i).toCharArray();

    for (int j =0;j<directions.length;j++) {
        if (directions[j] == 'N') {
            coords[i][0] --;
        }else if (directions[j] == 'S'){
            coords[i][0] ++;
        }else if (directions[j] == 'W'){
            coords[i][1] --;
        }else if (directions[j] == 'E'){
            coords[i][1] ++;                     /////THIS IS 36///// 
        }
    }
}

for (int i =0;i<coords.length;i++) {
     System.out.print("The "+i+". ship's coords: "+coords[i][0]+" ; "+coords[i][1]);
}

使用

ArrayList<String> control = new ArrayList<String>();

try {
    Scanner scan = new Scanner(robotok);

    while (scan.hasNextLine()) {
        String line = scan.nextLine();
        control.add(line);
    }
} catch(Exception e){
}

int coords[][] = new int[control.size()][2]; // index = robot sorszáma / [] Hosszúság / [] Szélesség

for (int i = 0;i<control.size();i++) {
    char[] directions = control.get(i).toCharArray();

    for (int j =0;j<directions.length;j++) {
        if (directions[j] == 'N') {
            coords[i][0] --;
        }else if (directions[j] == 'S'){
            coords[i][0] ++;
        }else if (directions[j] == 'W'){
            coords[i][1] --;
        }else if (directions[j] == 'E'){
            coords[i][1] ++;                     /////THIS IS 36///// 
        }
    }
}

for (int i =0;i<coords.length;i++) {
     System.out.print("The "+i+". ship's coords: "+coords[i][0]+" ; "+coords[i][1]);
}

问题是您打算在添加元素之前基于coords初始化control,其大小为0。稍后,在添加元素后,control变大,您尝试引用coords&#39;某些索引超过其大小的元素。因此例外。