If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Iterator

Hello everybody, this is my first post
here. I want to code an iterator for a
data structure homework. The instructor told
us that Iterator has to be a sperarate class.
Let's assume we have a doubly linkd list with nodes.

in main

Code:

iterator i = l.iterator();

(l is the name of the list)

then we use

Code:

for ( ; i.hasNext(); )
System.out.preintln(i.next());

Now in order to this work shouldnt
iterator be in the list class? How is the iterator
will get the position of the list without being in the
list? We are not passing any parameters to
the iterator. How do we keep track of
the data? The intructor told us that iterator should be
a speratae class and it has to have a List and
a node inside. I am very confused. i know this is
a very easy problem but I can't figure that out.

Doesn't something strike you as a little strange here? Use a while loop! You're performing no initialization and you're not performing any constant action each iteration. Therefore there's no point in using a for loop (and you're making it harder to read). I would have written:

Code:

while(i.hasNext()) {
System.out.println(i.next());
}

Note - I use braces even though there's only one line of code in the body of the loop. I recommend this as a good practice to get into - leaving them out will only get you into trouble later on.

Anyway, your question...

I think this will work best with an example, but I don't want to do your homework for you, so I'll produce my own problem:

Task:Create a BackwardsList class which simply holds a list of objects which can be added using the add(Object) method. You should provide a method iterator() which will allow you to iterator through the items added to the list in reverse order.

I know that this is the perfect example
to use a while-loop but when the instructor gave
that code I told him why not while and he
said that he wanted to see the whole
precess in one line.. If it were me I'd of course I'd use
while.

We didn't cove rinner classes. How do they work?
And I am not very clear about "this". I suppose
somehow you pass the argument ( the list) to the
iterator by calling bl.iterator() and that's "this"
sections of the constructors that handle it. Am I
correct?? We didnt cover this neither

huh? One line? Are you talking about the lack of braces? I think your instructor is talking rubbish. The choice of loop is determined by its use, nothing else. Instructors....sigh...

We didn't cove rinner classes. How do they work?

All it means is that you can put one class inside another and then prevent any other class from creating objects of that inner class. Don't worry about them for the moment.

And I am not very clear about "this".

It's probably best until you cover this in your course, but here's a two-minute tour of "this". OK, I could have written:

Code:

public BackwardsIterator(ArrayList theList) {
list = theList;

Here there would have been no problem, the parameter variable 'theList' is assigned to the instance variable 'list'.

This isn't a problem, but it often gets difficult to think up of new parameter variable names which are different from the instance variable names. They need to different:

Code:

public BackwardsIterator(ArrayList list) {
list = list;

In the above code the parameter variable and the instance variable are named the same thing: 'list'. In Java, variables are resolved as locally as possible meaning that both of the 'list' variables refer to the parameter variable, meaning that the following code is completely pointless:

Code:

list = list;

...since it's assigning itself to itself!

Right, so we need some way of saying that the 'list' on the left-hand side refers to the instance variable 'list' and the 'list' on the right-hand side refers to the parameter variable 'list. This is where 'this' comes in.
'this' refers to the current object. Therefore
'this.variableName' refers to the instance variable on the current object.

Now consider the code that I wrote:

Code:

public BackwardsIterator(ArrayList list) {
this.list = list;

The 'this.list' refers to the instance variable 'list' on the current object and the 'list' on the right refers to the parameter variable.

Thank you again!
It helps a lot. Now I started
learnign about trees and the book
we are using (Goodrich / Tomissa's Data
Sturctures and Algorithms in Java) is skipping
very importnat information. Could you recommend
a reference for that subject? I am having trouble
with

Ahh good old data structures and algorithms.
I hated that course sooo much....

code above they have 3 arguments

Maybe i am mistaken but the method with 3 arguments is called inorderPosition and the method u posted is called inorder. They are two different methods.

And adapters are just used to enable objects with different interfaces to communicate with each other. What you set for it is really dependent on the implementation but my guess is that the "adaptee", the object it will adapt to, is the positions array.

Maybe i am mistaken but the method with 3 arguments is called inorderPosition and the method u posted is called inorder. They are two different methods.

And adapters are just used to enable objects with different interfaces to communicate with each other. What you set for it is really dependent on the implementation but my guess is that the "adaptee", the object it will adapt to, is the positions array. [/B]

You are absouletly right. They give an example of
inorder and then they use inorderPosition in the
main code. So I m trying to figure out that one.
Isn't the goal of inorder functions to access all the
nodes(positions) and do stuff with them? I understand
with

Code:

if(isInternal(v))
inorder(t,t.leftChild(v));

we reach to an external node(with no child and we
are dealing with a binary tree here ) but after
the visit with

Code:

if(isInternal(v))
inorder(t, t.rightChild(v));

what's the purpose?

in the 3argument example I'm trying to write
an inorderPostions method that gets all the
positions and put them in the array.