Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Enhanced for loop question

 
Ranch Hand
Posts: 125
Scala Java Linux
  • 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?
 
Ranch Hand
Posts: 59
6
  • 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:

 
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: 59
6
  • 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
Scala Java Linux
  • 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?
 
    Bookmark Topic Watch Topic
  • New Topic