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

To use the enhanced for each loop on an object which of the following must be true?

 
Mark Moge
Ranch Hand
Posts: 100
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
question found at http://www.skillrack.com

To use the enhanced for each loop [available from Java 5 (or J2SE 1.5) onwards] on an object which of the following must be true?
  • The Object should be an Array
  • The Object should implement Collection interface
  • The Object should implement Iterator interface
  • The Object should implement Iterable interface
  •  
    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
    What is the answer according to you?? Did you try to find anything about this in any book or on the internet?? Checkout the documentation of java.lang.Iterable interface...
     
    Rajeev Trikha
    Ranch Hand
    Posts: 85
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    See iterable
     
    Mark Moge
    Ranch Hand
    Posts: 100
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok thanks for this. So the answer is
    The Object should be an Array or The Object should implement Iterable interface?

    for the record:
    Implementing java.lang.Iterable interface allows an object to be the target of the "foreach" statement.
    public interface Collection<E> extends Iterable<E>
    public final class Array extends Object
     
    Abimaran Kugathasan
    Ranch Hand
    Posts: 2066
    Clojure IntelliJ IDE Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mark Moge wrote:Ok thanks for this. So the answer is
    The Object should be an Array or The Object should implement Iterable interface?

    for the record:
    Implementing java.lang.Iterable interface allows an object to be the target of the "foreach" statement.
    public interface Collection<E> extends Iterable<E>
    public final class Array extends Object


    Hi,

    Other than Array, Any collections only can use the enhanced for loop? Please confirm!
     
    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
    Abimaran Kugathasan wrote:Other than Array, Any collections only can use the enhanced for loop? Please confirm!

    If you see the documentation of java.util.Collection, you'll see that it extends the Iterable interface. And as has been said in this thread before, any class/interface that implements Iterable can be used in the enhanced for loop, so the answer is yes...
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic