Jeff Verdegan wrote:Do you know what this line does?
Emanuel Mensa wrote:
Jeff Verdegan wrote:Do you know what this line does?
It's of type "Student" not "Object"...i edited the code.
Emanuel Mensa wrote:
so now i want to actually IMPLEMENT the custom iterator. but i don't know what to put after
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.
~ Mansukh
Mansukhdeep Thind wrote: Your custom class has to implement Iterable interface.
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 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.
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.
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.
~ Mansukh
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.
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.
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?
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
|