【问题标题】:What mean in for loop condition this line? [duplicate]这行的 for 循环条件是什么意思? [复制]
【发布时间】:2012-10-29 04:08:46
【问题描述】:

可能重复:
java for loop syntax

for (File file : files){
    ...body...
}

这一行的for循环条件是什么意思?

【问题讨论】:

    标签: java for-loop iteration


    【解决方案1】:

    这是 java for-each(或)增强的 for 循环

    这意味着对于文件数组中的每个文件(或)可迭代。

    【讨论】:

      【解决方案2】:

      for (File file : files) -- 是 Java 中的 foreach 循环,与 for each file in files 相同。其中files 是一个可迭代对象,file 是在 for 循环范围内临时存储的每个元素的变量。见http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

      【讨论】:

        【解决方案3】:

        这表示一组 fo 文件中的每个文件都执行...(正文)

        阅读The For-Each Loop 了解更多详情。链接中的示例很棒。

        【讨论】:

          【解决方案4】:

          - 这种形式的循环是从1.5 在Java 中出现的,被称为For-Each Loop

          让我们看看它是如何工作的:

          For循环:

          int[] arr = new int[5];    // Put some values in
          
          for(int i=0 ; i<5 ; i++){  
          
          // Initialization, Condition and Increment needed to be handled by the programmer
          
          // i mean the index here
          
             System.out.println(arr[i]);
          
          }
          

          For-Each 循环:

          int[] arr = new int[5];    // Put some values in
          
          for(int i : arr){  
          
          // Initialization, Condition and Increment needed to be handled by the JVM
          
          // i mean the value at an index in Array arr
          
          // Its assignment of values from Array arr into the variable i which is of same type, in an incremental index order
          
             System.out.println(i);
          
          }
          

          【讨论】:

            猜你喜欢
            • 2019-03-29
            • 1970-01-01
            • 1970-01-01
            • 2018-05-27
            • 2014-02-18
            • 2014-12-26
            • 2020-09-28
            • 2020-01-22
            • 2016-07-11
            相关资源
            最近更新 更多