Login Register






Enhanced For Loop filter_list
Author
Message
RE: Enhanced For Loop #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:

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.)

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 Oni - 09-10-2015, 01:36 AM