Possible Duplicate:
java for loop syntax
for (File file : files){
...body...
}
What mean in for loop condition this line?
Possible Duplicate:
java for loop syntax
for (File file : files){
...body...
}
What mean in for loop condition this line?
for (File file : files) -- is foreach loop in Java same as saying, for each file in files. Where files is an iterable and file is the variable where each element stored temporarily valid withing the for-loop scope. See http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html
This says for-each file in a set fo files do ... (body)
Read about The For-Each Loop for more details. The examples in the link where great.
- This form of loop came into existence in Java from 1.5, and is know as For-Each Loop.
Lets see how it works:
For Loop:
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 Loop:
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);
}