aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes To use the enhanced for each loop on an object which of the following must be true? 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 "To use the enhanced for each loop on an object which of the following must be true?" Watch "To use the enhanced for each loop on an object which of the following must be true?" New topic
Author

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

Mark Moge
Ranch Hand

Joined: Mar 14, 2009
Posts: 100

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


  • SCJP6 | http://programmerutilities.blogspot.com/
    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9313
        
      17

    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...


    SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
    Rajeev Trikha
    Ranch Hand

    Joined: Jan 29, 2010
    Posts: 85
    See iterable


    Rajeev Trikha (SCJP 6)
    Mark Moge
    Ranch Hand

    Joined: Mar 14, 2009
    Posts: 100

    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

    Joined: Nov 04, 2009
    Posts: 2066

    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!

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

    Joined: Aug 03, 2008
    Posts: 9313
        
      17

    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...
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: To use the enhanced for each loop on an object which of the following must be true?