RE: Enhanced For Loop 07-24-2014, 02:47 AM
#4
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...rable.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...rator.html
So you're code behind the scenes would be translated to something like the following:
@chmod you're thinking too functionally. for (int item : numbers) might look like a functional construct but its a shorthand notation.
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...rable.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...rator.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.
(This post was last modified: 07-24-2014, 02:49 AM by miiike980.)

![[+]](https://sinister.li/images/modern/collapse_collapsed.png)