【发布时间】:2015-02-25 16:27:26
【问题描述】:
如何操作for循环,问题在cmets上 如何重新进入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
}
}
}
}
【问题讨论】:
-
要改变循环变量的递增方式,只需手动递增变量,例如 i+=2。此外,您可能需要阅读休息并继续。
-
问题不是很清楚吗? “操纵for循环”和“动态添加索引,除了自动增量”是什么意思?
-
这个问题的格式很糟糕。请在“问题”而不是代码上提出问题!
-
“for 循环将增加 2,除了 auto inc”是什么意思?