- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试执行一个基本的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。您需要延迟创建它,直到列表达到最终值尺寸。
关于java - IndexOutofBoundsException:我无法解决它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30966081/
这个问题已经有答案了: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? (25 个回答)
((fn [coll] (letfn [(add-item [acc coll idx] (conj acc (nth coll idx))
我不是 Java 初学者,但也不是专家,这就是我发布此文章以寻求帮助/解释的原因。我在互联网上查找了很多地方,但没有找到我正在寻找的答案。 public class Driver { public s
我不断得到 线程“main”中出现异常 java.lang.ArrayIndexOutOfBoundsException:MergeSorter.merge(MergeSorter.java:44)、
我做了一个小射击游戏..它工作正常但我也想实现如果火相交它们会消失。我有两个播放器子弹列表和计算机子弹列表......但是如果我有更多来自计算机或反向的子弹。这是我的循环 for (int
我试图从 JOptionPane 读取像 3+9-2*10/5 这样的数学表达式并得到它的结果——当然要考虑到操作顺序。我使用 String.split() 将字符串拆分为数字和操作数,并创建了一个寻
我在Grails中有以下代码: public LibraryItem createLibraryItemWithValues(ProjectItem projectItem) { def li
这个问题已经有答案了: What is a debugger and how can it help me diagnose problems? (3 个回答) 已关闭 7 年前。 我有一组以下类型的
请看下面的代码 DatabaseHandler.java public List getDetails(String name) { List details = new Ar
我应该让蛇形矩形沿着屏幕的一侧移动。然而它保持在原位,当我运行它时,我还收到 IndexOutOfBoundsException 错误。我相信这与我的 Render.java 文件中的 for 循环有
ArrayList beds = new ArrayList(49); public Patient getPatient(int bedNumber) { if (beds.get(bedN
我有 10 张卡片的数组。在“回合”的过程中,我向数组添加一张卡片,然后从数组中删除一张卡片(并不总是相同的)。该代码在前 6-7 回合中效果很好,然后抛出 IndexOutofBoundsExcep
我正在尝试创建一个评分系统并获得最佳分数,但我的方法生成了 IndexOutOfBoundsException,但我找不到超出数组列表范围的内容,有人可以帮助我吗? 代码: public stati
为什么 int row = 1; // multiArray[col].length = 6 while(multiArray[col][row] > 1 && row 1) { 关于java -
我制作了一款游戏,我们控制一艘宇宙飞船,可以发射激光摧毁来自顶部的小行星。但在游戏过程中,每当它抛出 IndexOutOfBoundsException 时,我的游戏就会卡住。我不知道为什么会这样。请
我正在尝试实现合并排序,但我不断收到 IndexOutOfBoundsException 并且我无法找出原因。 我已经调试了我的程序,并且在此函数中我从未使用无效索引访问数组。引发异常的行也与抛出异常
我无法弄清楚为什么我的代码出现 IndexOutOfBoundsException。我想知道是否有人可以成为我的额外眼睛来帮助我发现错误。我正在手动尝试这个,但我认为在浏览代码时我遗漏了一些东西。 代
为什么我会收到此错误? Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 at java.util.
我不知道这是否是一个简单的问题,但我只是看不出问题是什么。我现在从 Google Play 中的应用收到了三份关于 points.get(++i) 处的 IndexOutOfBoundsExcepti
如何执行以下内容 String p = "abcd"; System.out.print(p.substring(4)); 不会导致java.lang.IndexOutOfBoundsExceptio
我是一名优秀的程序员,十分优秀!