【问题标题】:How to manipulate array如何操作数组
【发布时间】: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”是什么意思?

标签: java for-loop


【解决方案1】:

如果您的意思是增加循环索引,那么您可以增加代表索引的i 值。 i += 2; 应该做这项工作。当循环结束并重新开始时,它最终会增加 3。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多