Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enhanced for loop question

 
Laiq Ahmed
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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
 
rushikesh sawant
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it can be array or any collection.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I still dont know how extended for loop works for arrays since they dont implement iterable.
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • 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: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 3028
10
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks everyone for your input on this.
I missed the point earlier ... thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic