File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes help need on custom  Iteration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help need on custom  Iteration" Watch "help need on custom  Iteration" New topic
Author

help need on custom Iteration

Emanuel Mensa
Ranch Hand

Joined: Feb 22, 2013
Posts: 31
so i tried to create my own iterator.



one more interface:



and years my arraylist of some student class:



now my question is. how do i use my own custom iterator...to say, print all the elements in the "myClass" array?

i'm learning from a book and it doesn't show me how to do this.

the method to print all elements should look like this.





now what should replace "...." this is the most important thing i need
please help me guys. i need to understand this.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Do you know what this line does?

Emanuel Mensa
Ranch Hand

Joined: Feb 22, 2013
Posts: 31
Jeff Verdegan wrote:Do you know what this line does?



It's of type "Student" not "Object"...i edited the code.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Emanuel Mensa wrote:
Jeff Verdegan wrote:Do you know what this line does?



It's of type "Student" not "Object"...i edited the code.


Okay, but do you know what it does?

If you know what that line does, and you know what you're trying to do, then I don't see how you can wonder what comes next.

(I think there will still be another problem, but take one thing at a time.)
Emanuel Mensa
Ranch Hand

Joined: Feb 22, 2013
Posts: 31
well we want to have an iterator to traverse the array and print the list of all students in the array.

so when we have something like


we can go ahead and do something like:



clearer now?

so now i want to actually IMPLEMENT the custom iterator. but i don't know what to put after

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Emanuel Mensa wrote:
so now i want to actually IMPLEMENT the custom iterator. but i don't know what to put after



Whoops! My bad!

When you said you didn't know what to put for the "..." I didn't see those "...". I was looking at the ones after your call to current(), at the end of that code snippet.

The thing is, you're doing things a bit oddly--or at least differently than the Java Collections Framework does them. Your List class implements Iterator, so it is its own iterator. That's not usually the case. Since you've done it that way, you don't even need a separate Iterator variable. You can just use your List variable. If you want to have a separate Iterator variable (a good idea, IMHO), then you'd just set it equal to your List.

As for "I want to IMPLEMENT the iterator", no, not quite. You've already implemented it. When you filled in the bodies for all its methods, that was implementing it. Now you're trying to use it.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

The thing is, you're doing things a bit oddly--or at least differently than the Java Collections Framework does them. Your List class implements Iterator, so it is its own iterator. That's not usually the case.


Quite right. When I manually created my own ArrayList implementation, I too implemented the Iterable interface's iterator method. I then made a CustomIterator that implemented java.util.Iterator interface. So, it had implementations of hasNext(), next and remove() methods. I returned the CustomIterator instance from my iterator method. Emanuel is doing it differently in the sense that he actually has created his own Iterator interface with his own 5 methods. Anyways, no harm done learning something extra.

@Emanuel : You will use the Iterator that you created just like you would use the java.util.Iterator. Like this:



But I am not sure this is the way to go about the task. Your custom class has to implement Iterable interface. Then you have to implement its iterator method in which you would return an instance of a custom class that in turn implements the Iterator interface's 3 methods. But this has come into being only since Java 5. Before that, if you would want to do something like this, your custom class would have to implement the Collection interface's iterator method. In addition to that you would have to implement all the methods of java.util.Iterator or in your case your own Iterator.


~ Mansukh
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mansukhdeep Thind wrote: Your custom class has to implement Iterable interface.


Which it automatically does if it implements Collection.

But this has come into being only since Java 5.


Which is about 10 years ago maybe? Java 5 is already End-Of-Life, so unless you're stuck with some oddball legacy system, it's safe to assume that anything added in Java 5 will be available.

Before that, if you would want to do something like this, your custom class would have to implement the Collection interface's iterator method. In addition to that you would have to implement all the methods of java.util.Iterator or in your case your own Iterator.


I'm not sure what you're saying here, but I think you may be confused.

The Collections Framework, including Collection, List, Set, Iterator, etc., has been around since Java 1.2, around 1998 or 1999. The Iterable interface was, I think, added in Java 5. That doesn't affect at all how you write your own Collection and Iterator implementations. The only change that really brought in terms of class hierarchies is that the iterator() method was previously just defined directly on the Collection interface, but now its defined on the Iterable interface, and Collection extends Iterable.

Either way though, both pre- and post-1.5, if you're going to implement a Collection, you have to provide an iterator() method that returns an Iterator. Whether your Collection itself impelments Iterator also has nothing to do with what was added in 1.5. It's poor design to have your Collection implement Iterator, both before and after 1.5. Better to have a separate class.

This is only an educated guess, but I would expect that there were 2 main reasons for adding the Iterable interface.

1) Better design in general. Something shouldn't have to be a Collection to provide an Iterator. I can make any old class I want that implements Iterable, so I can call iterator() on it, without it having to provide all of Collection's methods. Other classes besides Collections can now have a notion of iteration.

2) To support foreach loops. Any class that implements Iterable can be used with the enhanced for loop syntax.


Note the difference:

Iterable : A class that can be iterated over. That is, one that has a notion of "get me the first thing, now the next thing, and so on, until we run out."

Iterator : A class that manages iteration over an iterable. That is, it keeps track of where we are in the current iteration, and knows what the next element is and how to get it.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Jeff Verdegan wrote:
Mansukhdeep Thind wrote: Before that, if you would want to do something like this, your custom class would have to implement the Collection interface's iterator method. In addition to that you would have to implement all the methods of java.util.Iterator or in your case your own Iterator.


I'm not sure what you're saying here, but I think you may be confused.


Jeff Verdegan wrote:The Collections Framework, including Collection, List, Set, Iterator, etc., has been around since Java 1.2, around 1998 or 1999. The Iterable interface was, I think, added in Java 5. That doesn't affect at all how you write your own Collection and Iterator implementations. The only change that really brought in terms of class hierarchies is that the iterator() method was previously just defined directly on the Collection interface, but now its defined on the Iterable interface, and Collection extends Iterable.

Either way though, both pre- and post-1.5, if you're going to implement a Collection, you have to provide an iterator() method that returns an Iterator. Whether your Collection itself implements Iterator also has nothing to do with what was added in 1.5. It's poor design to have your Collection implement Iterator, both before and after 1.5. Better to have a separate class.


Not confused Jeff. I meant that before Iterable came into being, one would have to implement the mandatory methods of Collection interface including Iterator<E> iterator. Then create a separate class (I also created a separate class called CustomIterator) that implements java.util.Iterator interface's 3 methods. (2 actually, remove() is optional)

Jeff Verdegan wrote:This is only an educated guess, but I would expect that there were 2 main reasons for adding the Iterable interface.

1) Better design in general. Something shouldn't have to be a Collection to provide an Iterator. I can make any old class I want that implements Iterable, so I can call iterator() on it, without it having to provide all of Collection's methods. Other classes besides Collections can now have a notion of iteration.

2) To support for-each loops. Any class that implements Iterable can be used with the enhanced for loop syntax.


Point 1 is taken. This specialized facility of being able to iterate over a collection had to be isolated from other operations like contains etc. But point 2 is where it really works its magic. Things are now much easier using the enhanced for loop.



Jeff Verdegan wrote:Note the difference:

Iterable : A class that can be iterated over. That is, one that has a notion of "get me the first thing, now the next thing, and so on, until we run out."

Iterator : A class that manages iteration over an iterable. That is, it keeps track of where we are in the current iteration, and knows what the next element is and how to get it.


Point noted. Iterable was more of an after thought I believe. If we had only 1 interface Iterable right from when Collection Framework was introduced way back in 1.2 release, which had all the 4 methods that the 2 interfaces have together, wouldn't that have been a better design choice? Their purpose is linked. You have to implement Iterator if you make your class Iterable. So why not follow design principle of cohesion and club these two? What do you think Jeff?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mansukhdeep Thind wrote:
Not confused Jeff. I meant that before Iterable came into being, one would have to implement the mandatory methods of Collection interface including Iterator<E> iterator.


Still have to do that. Nothing has changed.

Then create a separate class (I also created a separate class called CustomIterator) that implements java.util.Iterator interface's 3 methods.


Wasn't necessary then, and isn't necessary now. Both then and now, you didn't need a separate class. You can just have your Collection implementation implement Iterator, as the OP is doing here. That's not advisable, but again, nothing changes in that respect with the introduction of Iterable.

(2 actually, remove() is optional)


You still have to implement it. Optional just means it doesn't have to actually remove anything.

Jeff Verdegan wrote:This is only an educated guess, but I would expect that there were 2 main reasons for adding the Iterable interface.


Jeff Verdegan wrote:Note the difference:

Iterable : A class that can be iterated over. That is, one that has a notion of "get me the first thing, now the next thing, and so on, until we run out."

Iterator : A class that manages iteration over an iterable. That is, it keeps track of where we are in the current iteration, and knows what the next element is and how to get it.


Point noted. Iterable was more of an after thought I believe. If we had only 1 interface Iterable right from when Collection Framework was introduced way back in 1.2 release, which had all the 4 methods that the 2 interfaces have together, wouldn't that have been a better design choice? Their purpose is linked. You have to implement Iterator if you make your class Iterable. So why not follow design principle of cohesion and club these two? What do you think Jeff?


Even if Iterable had been there from the beginning, we'd still want Iterator. They are two distinct responsibilities. We want our collections to be iterable--we want to be able to iterate over them. But we DON'T want the collections themselves to be responsible for their own iteration.

The way it is now, with two different interfaces, is a good approach. It just would have been nice if they'd separated them at the beginning.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I didn't want to further detract from the OP's issue, so the side discussion about Collection/Iterator/Iterable has been split off to a new topic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help need on custom Iteration