aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enhanced for loop question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "enhanced for loop question" Watch "enhanced for loop question" New topic
Author

enhanced for loop question

Laiq Ahmed
Ranch Hand

Joined: Jan 04, 2010
Posts: 44
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

Joined: Dec 22, 2009
Posts: 65
it can be array or any collection.


SCJP 5.0 100%
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

You can iterate over arrays or anything that implements java.lang.Iterable...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Laiq Ahmed
Ranch Hand

Joined: Jan 04, 2010
Posts: 44
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

Joined: Nov 04, 2009
Posts: 2066

Only interface that array implements is Cloneable and Serializable.
Laiq Ahmed
Ranch Hand

Joined: Jan 04, 2010
Posts: 44
I still dont know how extended for loop works for arrays since they dont implement iterable.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Aug 03, 2008
Posts: 9304
    
  17

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

Joined: Mar 05, 2008
Posts: 3018
    
  10
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

Joined: Jan 04, 2010
Posts: 44
Thanks everyone for your input on this.
I missed the point earlier ... thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enhanced for loop question