Creating my own List interfaces - How to implement Iterator method?

Alright so for class we need to create our own versions of an ArrayList and double-linked-list, stack and queue.

We have a List ADT that we are using and one of the methods we need to implement is an Iterator.

The method looks like this:

public Iterator iterator()

{

}

But I have no idea what exactly I'm supposed to put in there. From what I can see Iterator is an interface itself in the API and it has its own methods.

So I guess all I want to do is make an iterator from my array and just return it.

Well this is what I tried:

public Iterator iterator()

{

iterator = array.iterator();

return iterator;

}

But that doesn't work, it keeps wanting to change everything to static.

If anyone knows how to go about doing this it'd be a huge help.

[1048 byte] By [Ultros_Maximusa] at [2007-10-2 12:58:54]
# 1

> So I guess all I want to do is make an iterator from my array and just return it.

What's "your array"? Is it an array member of your class? Do arrays have an iterator method (they don't)?

> But I have no idea what exactly I'm supposed to put in there. From what I can see Iterator is an interface itself in the API and it has its own methods.

Yes, it's an interface you can implement yourself, by implementing 2 or 3 methods. It's unclear how your class works, but if it stores the elements in an array you can implement your own Iterator using the array index.

Lokoa at 2007-7-13 10:17:19 > top of Java-index,Core,Core APIs...
# 2
To elaborate, you might create an inner class called MyIterator that implements Iterator to iterate over the array in your List implementation. Then the iterator() method would just need to return a new instance of that inner class.
kablaira at 2007-7-13 10:17:19 > top of Java-index,Core,Core APIs...
# 3
> But that doesn't work, it keeps wanting to change> everything to static.What do you mean by the above? Have you declared array as static?
dubwaia at 2007-7-13 10:17:19 > top of Java-index,Core,Core APIs...
# 4
This is ultros_maximus (can't seem to log into that account)Anyway, I ended up just creating an inner class, and it worked.Thanks for the help.
Robmana at 2007-7-13 10:17:19 > top of Java-index,Core,Core APIs...