Results 1 to 6 of 6
Thread: Using an iterator on a Vector
- 12-09-2010, 03:38 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 45
- Rep Power
- 0
Using an iterator on a Vector
I am studying vectors and iterators and in the sample code I am looking at, a for loop is used and starts by initializing a variable by assigning a vector object's methods return value, which is an Iterator object, using the iterator method. Here is the code:
I understand that the class Iterator is an interface, and that it only contains abstract methods for remove(), hasNext() and next(). I thought that the iterator() method of the vector, which is apparently inherited from the superclass List, would return an object containing those methods because the methods must be defined to be used by the new Iterator object.Java Code:Vector v = new Vector(3); v.add(1); v.add(2); v.add(3); for ([B]Iterator i = v.iterator(); [/B]i.hasNext(); ) { String name (String)i.next(); System.out.println(name); }
So does the iterator() method of the vector return an object that has inherited the remove(), hasNext(), and next() methods? I couldn't find the code on Oracle's website that those three methods were defined. Thanks for your help!
-Derek Raimann
- 12-09-2010, 03:58 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Vector extends AbstractList which implments the iterator() method. This method returns a object of a class called Itr ( a private class in AbstractList !) which implements the Iterator interface!So does the iterator() method of the vector return an object that has inherited the remove(), hasNext(), and next() methods? I couldn't find the code on Oracle's website that those three methods were defined. Thanks for your help!
- 12-09-2010, 04:19 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 45
- Rep Power
- 0
I'm trying to follow what you're saying as best as I can, but I can't find the code myself. This is the link to where I am looking at code:
Java Platform SE 6
On the left hand side of the screen, second window down, I clicked AbstractList to look it its methods. It does say that it creates an iterator method, but AbstractList is an abstract class so it can't implement those methods right? From what I learned, the definitions of methods from an abstract class would be defined in a subclass of the abstract class itself. I'm missing something or not up to your level yet to understand where the code iis coming from. I just expected to see those methods defined in a subclass of AbstractList after reading your post. Thanks again for bearing with me!
-Derek Raimann
- 12-09-2010, 04:28 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
that is only the documentation, you will not find any code there. src.zip in your jdk folder contains as an example the source code, or download it from the oracle page!
No, that`s wrong! It can! That is the reason why Vector or other subclasses of AbstractList like ArrayList have that method (iterator() )On the left hand side of the screen, second window down, I clicked AbstractList to look it its methods. It does say that it creates an iterator method, but AbstractList is an abstract class so it can't implement those methods right?
AbstractList implementation of iterator():
Itr is a private class which implements Iterator interface!Java Code:public Iterator<E> iterator() { return new Itr(); }
- 12-09-2010, 04:30 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Nope, all an abstract class has to do to define itself as abstract, is, well, define itself abstract:
The above class is an abstract class because it says so; you can't instantiate an object of class Foo and you have to extend it to instantiate an object of the extended class. Everything can use the public methods defined in this abstract class though.Java Code:abstract public class Foo { // defined and implemented methods here and the entire shebang ... }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-09-2010, 04:40 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 45
- Rep Power
- 0
Similar Threads
-
Iterator as return and argument or Iterator-String(not visible in the test file)
By Aldarius in forum New To JavaReplies: 0Last Post: 05-18-2010, 12:53 AM -
Vector<vector> loop thru
By ocean in forum New To JavaReplies: 11Last Post: 11-21-2009, 02:17 PM -
Iterator help
By alpdog14 in forum New To JavaReplies: 2Last Post: 10-13-2009, 08:42 PM -
iterator
By venkatallu in forum Advanced JavaReplies: 3Last Post: 09-23-2008, 01:32 PM -
using Iterator with Vector
By Java Tip in forum Java TipReplies: 0Last Post: 11-13-2007, 10:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks