Sinisterly
Enhanced For Loop - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Java, JVM, & JRE (https://sinister.ly/Forum-Java-JVM-JRE)
+--- Thread: Enhanced For Loop (/Thread-Enhanced-For-Loop)



Enhanced For Loop - blackeagle - 07-23-2014

hey i can understand the for Loop really well but i never unrstood how the Enhanced For Loop works !!
public static void main(String[] args){
int[] numbers =
{1,2,3,4,5,6,7,8,9,10};
for (int item : numbers) {
System.out.println("Count is: " + item);
}

here's and example i know what this code do but i don't know the logic how it works !!

(int item : numbers)i need to know how this work pls !!

@Riverclawz thank you for helping me figure out ho it works on irc i appreciate it !!

pls feel free to close this thread !!


RE: Enhanced For Loop - chmod - 07-24-2014

I'm not a Java programmer and someone else will likely be able to answer this much better than I can but here's what looks like is happening.
Code:
int[] numbers =
{1,2,3,4,5,6,7,8,9,10}
This is an array with 10 items in it

Code:
for (int item : numbers) {
System.out.println("Count is: " + item);

for (int item : numbers) {
takes the current item in the array

System.out.println("Count is: " + item);

prints out the item in the array (first item being 1) and adds one to item so the next time the loop runs the second item in the array is printed and so on.

That's how it looks from a general programming perspective anyway.


RE: Enhanced For Loop - blackeagle - 07-24-2014

true it keeps doing this till the array ends !!


RE: Enhanced For Loop - dropzon3 - 07-24-2014

That for loop is called a 'for-each loop'

It is is a shorthand notation for writting a loop that acts over an 'iterable collection'

That is to say the object (numbers in this case) need to implement the Iterable interface http://docs.oracle.com/javase/7/docs/api/java/lang/Iterable.html

Which just means that it provides a standard interface to looping over all the objects in the collection(array). Basically it provides a nice object that has methods next() hasNext() and remove() for you to loop over. Look at the Iterator interface for more information: http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html

So you're code behind the scenes would be translated to something like the following:

Code:
Iterator<Int> ite = numbers.iterator();
while(ite.hasNext()) {
   int item = ite.next();
   System.out.println("Count is: " + item);
}

@chmod you're thinking too functionally. for (int item : numbers) might look like a functional construct but its a shorthand notation.


RE: Enhanced For Loop - Psycho_Coder - 07-24-2014

Preferably it should be called as For-each loop. These links have good explanations :-
http://www.javatpoint.com/for-each-loop

http://www.leepoint.net/notes-java/flow/loops/foreach.html

http://www.javapractices.com/topic/TopicAction.do?Id=196


RE: Enhanced For Loop - Banadmin - 06-11-2015

Enhanced for loops are used when the programmer wants to iterate through every element within the array. Just thought I'd throw that in there. Smile


RE: Enhanced For Loop - Coca. - 09-09-2015

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.


RE: Enhanced For Loop - Dismas - 09-10-2015

(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