This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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

Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

Question is from certpal
Which of the following are true regarding this loop ?

for (Object o : i)
{}

Choose: 2

Options

1.) This is an enhanced for loop

2.) i may be an Integer[], Object[], iterator, or a Comparable

3.) There is more than one syntax for this loop

4.) for (Object o : list) where list is an ArrayList will compile

5.) for (Object o : list.iterator()) where list is an ArrayList will compile

6.) This loop is available in java 1.4 and above

Answers are 1 and 4


What I don't understand is this statement from the Explanation:
A comparable that returns an Iterator can however be used in this loop.


I'm not able to understand this line. Can anyone please give an example explaining it?


OCPJP 6 - 96%
Sresh Rangi
Ranch Hand

Joined: Nov 28, 2012
Posts: 45
    
    1
Enhanced for loops can be used with Iterables or Arrays. A comparable isn't iterable by itself, but can become so by implementing Iterable as well as Comparable. By implementing Iterable it can return an Iterator in a way that can be used in the enhanced for loop:

Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 245

But why does the object needs to be Comparable to user this loop? What it needs to be compared here?


Astha - OCPJP 6 (90%)
Sresh Rangi
Ranch Hand

Joined: Nov 28, 2012
Posts: 45
    
    1
Astha Sharma wrote:But why does the object needs to be Comparable to user this loop? What it needs to be compared here?


It doesn't need to be Comparable for the for loop. The explanation is just saying that being comparable doesn't prevent it from being used, because it can also be iterable.
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 245

Sresh Rangi wrote:
Astha Sharma wrote:But why does the object needs to be Comparable to user this loop? What it needs to be compared here?


It doesn't need to be Comparable for the for loop. The explanation is just saying that being comparable doesn't prevent it from being used, because it can also be iterable.


Oh...got it...thanks
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

Sresh Rangi wrote:Enhanced for loops can be used with Iterables or Arrays. A comparable isn't iterable by itself, but can become so by implementing Iterable as well as Comparable. By implementing Iterable it can return an Iterator in a way that can be used in the enhanced for loop:



I think I understood what that statement meant, though partially. Can you please post the whole code for this program including the skipped parts?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enhanced for loop question
 
Similar Threads
Enhanced for loop
Regarding generics, mixed with legacy collection code.
Final modifier in enhanced for-loop : How can this be?
Doubt i a generics question
ArrayList with Iterator new*