[英]IndexOutOfBoundsException on arraylist java
I'm looking for the equivalent word in the database by the ContextQuery method, and when a equivalent word is null the program must try to use the next index from words and add it up to the current to make it a two word, if the two word is still null the program will make it a three word looking for the next 2 value, the equivalent is now being printed in console but i have the error IndexOutOfBoundsExpection after running 我正在通过ContextQuery方法在数据库中寻找等效单词,当等效单词为null时,程序必须尝试使用单词中的下一个索引,并将其加到当前单词中,以使其成为两个单词,如果两个单词仍然为空,程序将使其变成三个单词,寻找下一个2值,现在正在控制台中打印等效单词,但运行后出现错误IndexOutOfBoundsExpection
for (int i = 0; i < words.size(); i++){
temp = QueryWithContext.query(words.get(i));
if((temp == null || temp.isEmpty()) && words.size() >= i+1)
{
QueryWithContext.query(words.get(i)+" "+words.get(i+1));
temp = QueryWithContext.query(words.get(i)+" "+words.get(i+1));
System.out.println("1st if");
if((temp == null || temp.isEmpty()) && words.size() >= i+2)
{
temp = QueryWithContext.query(words.get(i)+" "+words.get(i+1)+" "+words.get(i+2));
}
else
{
temp = words.get(i);
}
}
System.out.println(temp);
if((temp == null || temp.isEmpty()) && words.size() >= i+1)
must be 一定是
if((temp == null || temp.isEmpty()) && words.size() > i+1)
otherwise 除此以外
words.get(i+1)
throws the IndexOutOfBoundsExpection. 抛出IndexOutOfBoundsExpection。
The problem is most likely in this line: temp = QueryWithContext.query(words.get(i)+" "+words.get(i+1)+" "+words.get(i+2));
此行最有可能出现问题: temp = QueryWithContext.query(words.get(i)+" "+words.get(i+1)+" "+words.get(i+2));
. 。 You are looping until i
is less than the size of words
, so i
will range from 0 to n - 1
. 您一直循环播放,直到i
小于words
的大小,因此i
范围是0到n - 1
。
The problem is that in your code, you keep going till i + 2
(and previously, i + 1
). 问题在于,在您的代码中,您将一直进行到i + 2
(以前是i + 1
)。 This is what is most likely causing your error. 这是最有可能导致您的错误的原因。 To fix this, see if you can do the following: for (int i = 0; i < (words.size() - 2); i++){
要解决此问题,请查看是否可以执行以下操作: for (int i = 0; i < (words.size() - 2); i++){
Alternatively, do as @Uli recommends. 或者,按照@Uli的建议进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.