【发布时间】:2012-10-29 04:08:46
【问题描述】:
【问题讨论】:
【问题讨论】:
这是 java for-each(或)增强的 for 循环
这意味着对于文件数组中的每个文件(或)可迭代。
【讨论】:
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
【讨论】:
这表示一组 fo 文件中的每个文件都执行...(正文)
阅读The For-Each Loop 了解更多详情。链接中的示例很棒。
【讨论】:
- 这种形式的循环是从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);
}
【讨论】: