我正在通过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)


一定是

if((temp == null || temp.isEmpty()) && words.size() > i+1)


除此以外

words.get(i+1)


抛出IndexOutOfBoundsExpection。

10-07 14:41
查看更多