RE: Enhanced For Loop 09-10-2015, 01:36 AM
#8
(09-09-2015, 11:45 PM)Coca. Wrote: Enhanced for-loops iterate through every element in the specified array unless the loop is stopped (using break or another control flow modification).
Code:int[] array = {1, 2, 3};
for(int i : array){
// current iteration
}
In the above example, the enhanced for-loop can execute only three times since the size of the array is 3. The local variable "i" in the enhanced for-loop scope holds the currently iterated element. The elements are iterated in the order of the array. If the enhanced for-loop is on its 2nd iteration, for example, the "i" local variable is equivalent to "2".
Here is a demonstration.
Code:for each integer in the array, iterating in order, setting the local variable named "i" to the current iterating index value,{
do this
}
You can use the enhanced for-loop with any class inheriting from "Iterator"; which is how Java Collections work with them.
I'd delete this for grave-digging, but it's quality.
![Tongue Tongue](https://sinister.ly/images/smilies/set/tongue.png)
![[Image: fSEZXPs.png]](https://i.imgur.com/fSEZXPs.png)