如何操纵for循环,问题在于评论 如何重新进入for循环并动态添加索引,除了自动增量。
for (int i = 0; i < words.size(); i++)
{
try {
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 condition = true, the codes below must not run anymore
//and for loop will increment by 1 except for the auto inc
if((temp == null || temp.isEmpty()) && words.size() > i+2)
{
System.out.println("2nd if");
temp = QueryWithContext.query(words.get(i)+" "+
words.get(i+1)+" "+words.get(i+2));
//if condition = for loop will increment by 2
//except for the auto inc
}
}
}
}
答案 0 :(得分:1)
如果您的意思是增加循环索引,则可以增加代表索引的i
值。 i += 2;
应该做的工作。当循环结束并重新开始时,它最终将增加3。