我知道这个问题已经被问过一百万次了。我觉得这个解决方案对于那些没有盯着它几个小时的人来说是相当明显的。但我无法弄清楚我的越界异常。这是错误:
exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 207493, Size: 207493
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at affysureselect.AffySureSelect.main(AffySureSelect.java:92)
Java Result: 1
我在想,这可能是由于数组列表的大小而发生的,但如果是这样的话,我会预计错误是在添加时出现,而不是在获取时出现。这是它即将死亡的代码:
String chrompos;
ArrayList<String> chromnum = new ArrayList<String>();
while ((input2 = sbuff.readLine()) != null) {
prse = input2.split("\t");
chromnum.add(prse[0]);
...
chrompos = prse[7];
}
int cnt = 0;
int cnt2 = 0;
if (chromnum.get(cnt).equals(chrompos)) { // line causing my untimely death
end = Integer.parseInt(chromposend.get(cnt2));
start = Integer.parseInt(chromposstart.get(cnt2));
...
我什至尝试添加:
if (cnt <= chromnum.size()) { //this line
if (chromnum.get(cnt).equals(chrompos)) { /before the dying line
但它无论如何都会死掉,在获取时,而不是在添加时。我错过了什么?
最佳答案
如果您要增加 cnt,请确保它始终小于 chromnum.size()。
应该是-
if (cnt < chromnum.size())
关于Java 数组列表越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15825574/