Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Enhanced For Loop filter_list
Author
Message
RE: Enhanced For Loop #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
[Image: fSEZXPs.png]

Reply





Messages In This Thread
Enhanced For Loop - by blackeagle - 07-23-2014, 11:22 PM
RE: Enhanced For Loop - by Banadmin - 06-11-2015, 04:50 PM
RE: Enhanced For Loop - by Coca. - 09-09-2015, 11:45 PM
RE: Enhanced For Loop - by Dismas - 09-10-2015, 01:36 AM



Users browsing this thread: 1 Guest(s)