Results 1 to 6 of 6
  1. #1
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default 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:

    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);
    }
    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.

    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

  2. #2
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    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!
    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!

  3. #3
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    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

  4. #4
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by DerekRaimann View Post
    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
    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!

    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?
    No, that`s wrong! It can! That is the reason why Vector or other subclasses of AbstractList like ArrayList have that method (iterator() )

    AbstractList implementation of iterator():
    Java Code:
        public Iterator<E> iterator() {
    	return new Itr();
        }
    Itr is a private class which implements Iterator interface!

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DerekRaimann View Post
    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?
    Nope, all an abstract class has to do to define itself as abstract, is, well, define itself abstract:

    Java Code:
    abstract public class Foo {
       // defined and implemented methods here and the entire shebang ...
    }
    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.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Smile

    I misunderstood the author of the book and it sounded like he was saying you could only declare but not implement methods in abstract classes. I am glad to know that isn't so and where the method I was looking for actually comes from. Thanks again for your help!

    -Derek Raimann

Similar Threads

  1. Replies: 0
    Last Post: 05-18-2010, 12:53 AM
  2. Vector<vector> loop thru
    By ocean in forum New To Java
    Replies: 11
    Last Post: 11-21-2009, 02:17 PM
  3. Iterator help
    By alpdog14 in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 08:42 PM
  4. iterator
    By venkatallu in forum Advanced Java
    Replies: 3
    Last Post: 09-23-2008, 01:32 PM
  5. using Iterator with Vector
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-13-2007, 10:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •