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 Beginning Java and the fly likes Help with an Iterator question 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 » Java » Beginning Java
Bookmark "Help with an Iterator question" Watch "Help with an Iterator question" New topic

Help with an Iterator question

Vineeth Menon
Ranch Hand

Joined: Aug 08, 2011
Posts: 67

I have been learning Java for 5-6 months, my friend passed me this question regarding iterator for the first time in my life I have no clue what this means, can someone help me?

[Fixed code tags and formatting - see UseCodeTags for details]

Anayonkar Shivalkar

Joined: Dec 08, 2010
Posts: 1502

Hello Jim Fields,

Welcome to CodeRanch!

Please, UseCodeTags. I can see a closing code tag, however, since, there is no opening code tag, the code snippet is quite difficult to read.

Also, please TellTheDetails. What exact part you are having trouble with? And what way of thinking you are following as of now?

I hope this helps.

Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
S.R Paul
Ranch Hand

Joined: Mar 22, 2012
Posts: 30

Hi Jim, The code itself doesn't make sense, since mostly no usable body in it.
What it is actually tying to do is, create a customized Iterator class, but lack of body code, nothing more.
All the collection interfaces implements Iterator, and tats just enough for normal use.

Next time try to post your code bit formatted

=-- Anything less than immortality is a complete waste of time --=
Matthew Brown

Joined: Apr 06, 2010
Posts: 4344

Hi Jim. Welcome to the Ranch!

OK, what you've got there is a skeleton implementation of a custom Iterator. It looks like it's intended to wrap an existing iterator and add some additional functionality. Although as far as I can tell at first glance the additional functionality is just to optionally eliminate duplicates.

So the first question is, do you know what an Iterator is? If not, you need to read about those. If you do, you need to work out how to implement this one - you need to delegate to the wrapped Iterator, but then add whatever additional logic you need. What I'd suggest is as a first step get it delegating completely, so your Iterator works exactly the same as the one passed in. Then worry about the duplicates once you've got that working.

(A quick observation: getting a rough solution looks relatively straightforward. Getting a perfect one not so simple - I can see one tricky point).
Vineeth Menon
Ranch Hand

Joined: Aug 08, 2011
Posts: 67

Thanks a lot for the input guys, I guess I need to find out a way to implement the iterator functionality in this skelital implementation of this that it???
Sammy Wilson

Joined: May 28, 2012
Posts: 1
Here is my solution:

[Moderator action: Solution removed. This site is NotACodeMill.(⇐click)]
Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

Vineeth Menon wrote:Thanks a lot for the input guys, I guess I need to find out a way to implement the iterator functionality in this skelital implementation of this that it???

Well, since we have:

I would say, yes, you need to do exactly that, as it appears to be the whole point of the exercise.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38062
Sammy Wilson, welcome to the Ranch

Please note what it says on the contents page for this forum:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
That is why Jeff removed your post: it does not help people to be given too complete a solution too quickly.
I agree. Here's the link:
subject: Help with an Iterator question
Similar Threads
Java Collections and the Iterator Pattern
My LinkedList Iterator - Can't print first element - No use of Java API
Iterator Pattern
about Class AbstractCollection