if(line.get(count).substring(line.get(count).indexOf(p1)).equals(p1.substring(0, p1.indexOf(" ")))) {
count++;
if(line.get(count).contains(p1.substring(p1.indexOf(" ") + 1, p1.length())))
System.out.println((count-1) + " " + (line.get(count-1).indexOf(p1)) + " " + p1);
}
Line是大小为65008的数组列表,我试图测试一个名字出现在文件的两行,第一行出现在第一行,最后一个名字出现在第二行。但是,索引超出范围的异常发生在第一个if语句中。我无法意识到如果我在这里做错了导致这个问题,因为我试图比较子串。可以有人提出解决这个问题的方法吗?这些if语句很长,但我稍后会尝试调整它们。
答案 0 :(得分:0)
尝试使用此代码:
if (line.get(count).substring(line.get(count).indexOf(p1))
.equals(p1.substring(0, p1.indexOf(" ")))) {
if (line.get(count).contains(
p1.substring(p1.indexOf(" ") + 1, p1.length()))) {
System.out.println((count - 1) + " "
+ (line.get(count - 1).indexOf(p1)) + " " + p1);
}
count++;
}
主要区别在于,如果条件已经运行,它会增加计数,如果为了可读性,还会向第二个添加括号。我不确定这会解决问题,如果没有,那么您可能需要提供更多信息。