This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enhanced for loop question

 
Sidharth Khattri
Ranch Hand
Posts: 125
Java Linux Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sresh Rangi
Ranch Hand
Posts: 52
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why does the object needs to be Comparable to user this loop? What it needs to be compared here?
 
Sresh Rangi
Ranch Hand
Posts: 52
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
Java Linux Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic