| 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: 96
|
|
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 ArrayThe Object should implement Collection interfaceThe Object should implement Iterator interfaceThe Object should implement Iterable interface
|
SCJP6 | http://programmerutilities.blogspot.com/
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
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: 96
|
|
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
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
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...
|
 |
 |
|
|
subject: To use the enhanced for each loop on an object which of the following must be true?
|
|
|