• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

help need on custom Iteration

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you know what this line does?

 
Emanuel Mensa
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



It's of type "Student" not "Object"...i edited the code.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic