This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Iterator problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Iterator problem" Watch "Iterator problem" New topic

Iterator problem

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178

Dear friends,
This code gives a compiler error: Can only iterate over an array or an instance of java.lang.Iterable

Kindly provide me the explanation of this compiler error

Paul Michael
Ranch Hand

Joined: Jul 02, 2001
Posts: 697
The enhanced for loop in Java 5 works with the new Iterable interface which encapsulates an Iterator.

In your example, you are returning an Iterator instead of an Iterable.

SCJP 1.2 (89%), SCWCD 1.3 (94%), IBM 486 (90%), SCJA Beta (96%), SCEA (91% / 77%), SCEA 5 P1 (77%), SCBCD 5 (85%)
wood burning stoves
subject: Iterator problem
Similar Threads
doubt with for-each loop
problem with advance for loop
unable to figure out the error
Printing a List backwards