• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

enhanced for loop question

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question regarding enhanced for loop in Java 5 and above. To use the it on an object, what is the requirement for that object?

should be an Array, should implement Collection interface, should implement Iterator or should implement Iterable ??

I have been using it on arrays only.

Thanks
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it can be array or any collection.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The enhanced for loop is used to simplifies the looping through the arrays and collections(data structures in java that stores objects and iterates them). It's simply a Flow control. We don't need to implements any interfaces to use it. Even though there are some restrictions.

For Ex:


The variable should declare it on the same line.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can iterate over arrays or anything that implements java.lang.Iterable...
 
Laiq Ahmed
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:You can iterate over arrays or anything that implements java.lang.Iterable...



I agree with your comment. But we can use extended for loop on arrays also. Do they also implement Iterable?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only interface that array implements is Cloneable and Serializable.
 
Laiq Ahmed
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still dont know how extended for loop works for arrays since they dont implement iterable.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Laiq Ahmed wrote:I still dont know how extended for loop works for arrays since they dont implement iterable.


Read Ankit's post again.

Ankit Garg wrote:You can iterate over arrays or anything that implements java.lang.Iterable...


Note the word in bold - I added the bold, but the word was there already. Arrays and Iterables are two different cases, and Sun made it so you could loop over either one. You can loop through arrays because the folks at Sun decided you should be able to loop over arrays, since it's an extremely common case. They write the compiler, and the JVM, so they simply coded it so looping through arrays with enhanced for would work. They can do that.

If they were desigining (or redesigning) Java today, I suspect there's a good chance they would have made arrays implement Iterable. Well, if they had arrays at all - they're kind of redundant if you also have Lists. But Java was designed long ago, and some decisions were made long ago that may seem questionable now. Yet it's too late for Sun or Oracle to simply change these old decisions without creating a lot of problems for programmers trying to maintain existing software. So they don't - usually.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Laiq Ahmed wrote:I still dont know how extended for loop works for arrays since they dont implement iterable.


It shouldn't be that hard to understand. The for-each loop can be used to iterate over arrays and anything that implements java.lang.Iterable. Why do arrays have to implement Iterable?? I didn't say that for-each loop can iterate over only things that implement Iterable. The for-each loop iterates overs arrays using the normal index based access and uses an iterator to iterate over anything else...

[Edit: beaten to the answer, I'm getting too slow ]
 
Mike Simmons
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:[Edit: beaten to the answer, I'm getting too slow ]


To be fair, the answer was already contained in your previous post.
 
Laiq Ahmed
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for your input on this.
I missed the point earlier ... thanks again.
reply
    Bookmark Topic Watch Topic
  • New Topic