IndexOutofBoundsException:我无法解决它

IndexOutofBoundsException: I cant solve it

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

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

得到以下问题:

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]);
}

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

当您创建控件 ArrayList 时,其初始长度为 0,并且在访问其长度之前不会在其中插入任何元素 因此,坐标的数组长度也为零,您正试图访问超出其长度的数组 所以,建议在赋值给lenght

之前插入要控制的值

而不是

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]);
}

问题是您打算在添加元素之前根据大小为 0 的 control 初始化 coords。稍后,添加元素后,control 变大,您尝试在某些索引超过其大小的位置引用 coords' 元素。因此例外。