Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
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
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: 186

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%)
I agree. Here's the link:
subject: Iterator problem
It's not a secret anymore!