Results 1 to 14 of 14
  1. #1
    Harb is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default Implementing Iterator

    Hello.
    I'm trying to make a class that I want to be able to use in a "foreach" statement. My question is, how does the object know when to start over in a new iteration? (i.e. next time I want to iterate through the collection)

    I'm using a counter with the next() method to keep track of which object it is currently on, but I'm unsure of which method is supposed to reset the counter to start over from the beginning.

  2. #2
    henry_78 is offline Member
    Join Date
    Aug 2007
    Posts
    47
    Rep Power
    0

    Default Iterator

    Hi,
    First of all, Iterator is a java interface and it has methods to iterate on elements of collections.
    Un can create your own iterator implementing that interface:
    The methods of the interface are:
    hasNext() : To know if there other elements left in the collection.
    next() : Returns the next element.
    remove() : To remove the current element from the collection(better not use that metthod).

    U can read more about the Iterator in the java doc.
    Bye.

  3. #3
    Harb is offline Member
    Join Date
    Aug 2007
    Posts
    2
    Rep Power
    0

    Default

    Thanks for the reply, and sorry for not being very clear in my first post.

    So far I have:

    Java Code:
    public class MyClass implements Iterable<AnotherClass>, Iterator<AnotherClass> {
    count = 0;
    AnotherClass[] collection;
    
    public MyClass(){
    }
    
    
    //constructors and other stuff here
    ...
    
    
    public Iterator<AnotherClass> iterator() {
        return this;
    }
    
    public boolean hasNext() {
        return count < collection.size();
    }
    
    public AnotherClass next() {
        if(count == collection.size()){
                throw new NoSuchElementException();
        }
        return collection[count];
        }
    
    public void remove() {
        throw new UnsupportedOperationException();
    }
    And according to the example I've read on another site, this should work. What I don't understand though, is when/where should i reset the count variable? (I realize the class I've presented is pretty useless, I'm using it merely as an example) If I iterate through the collection once, the count variable will be incremented until it reaches the end of the collection. And if I want to iterate through it again, it seems to me I'll have a problem :P
    Last edited by Harb; 08-04-2007 at 07:55 PM.

  4. #4
    henry_78 is offline Member
    Join Date
    Aug 2007
    Posts
    47
    Rep Power
    0

    Default

    Hi,
    In this implementation u have to increment 'count' when u invoke the next() method, this will help u to know when your collection is finished.
    As u can see in your code, the hasNext() method verifies that
    count < collection.size();
    To iterate another time, u have to create another itaerator...or change your class adding more methods.
    Bye.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Seems like you would reset the count after the last element was sent out.
    Java Code:
    public AnotherClass next() {
        if(count == collection.size()){
                throw new NoSuchElementException();
        }
        if(count == collection.size()-1) {
            // last element being sent out.
            count = 0;
            return collection[collection.size()-1];
        }
        return collection[count];
    }

  6. #6
    henry_78 is offline Member
    Join Date
    Aug 2007
    Posts
    47
    Rep Power
    0

    Default

    If u do like that, u wont even know when u reach the end of the collection.
    When a n iterator reach the end of a collection it stops..it dosen't go back to the first element.
    U should change only the next() method like that:

    public AnotherClass next() {
    if(count == collection.size() ){
    throw new NoSuchElementException();
    }
    count++;
    return collection[count];
    }

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    If u do like that, u wont even know when u reach the end of the collection.

    Right.

  8. #8
    anahuacv is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    @henry_78 Your solution is not ok, because you will never return the element in position 0, i.e. you are incrementing the counter before returning the element.

    public AnotherClass next() {
    if(count == collection.size() ){
    throw new NoSuchElementException();
    }
    count++;
    return collection[count];
    }

    Solution:

    public AnotherClass next() {
    if(count == collection.size() ){
    throw new NoSuchElementException();
    }
    return collection[count++];
    }

    When you receive the NoSuchElementException you might want to call a reset method. i.e.

    public boolean reset() {
    return count 0;
    }

    The idea of creating a new iteration sucks, 'cause Java is slow as a turtle (its advantage: easy to use), so try to do things as efficient as possible no matter is not the java way :-).

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default

    Make your Iterator implementation another (inner) class with its own count variable; as it is now you can only have one iterator at any time; (try to get two Iterators and you see what I mean).

    kind regards,

    Jos

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This thread's a bit long in the tooth, but it's an interesting discussion nevertheless.

  11. #11
    anahuacv is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by anahuacv View Post
    @henry_78 Your solution is not ok, because you will never return the element in position 0, i.e. you are incrementing the counter before returning the element.

    public AnotherClass next() {
    if(count == collection.size() ){
    throw new NoSuchElementException();
    }
    count++;
    return collection[count];
    }

    Solution:

    public AnotherClass next() {
    if(count == collection.size() ){
    throw new NoSuchElementException();
    }
    return collection[count++];
    }

    When you receive the NoSuchElementException you might want to call a reset method. i.e.

    public boolean reset() {
    count 0;
    }

    The idea of creating a new iteration sucks, 'cause Java is slow as a turtle (its advantage: easy to use), so try to do things as efficient as possible no matter is not the java way :-).
    I fixed a little mistake.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default

    Resetting that count variable doesn't help you one bit when you have more than one Iterator instantiated; they'll share that variable and you don't want that. Build a separate class for the Iterator that has its own count variable. It is a bad design smell if a class is more than one thing, here: an Iterable and an Iterator. Those two things are two opposites, don't cram them in one single class.

    kind regards,

    Jos

  13. #13
    anahuacv is offline Member
    Join Date
    Nov 2009
    Posts
    3
    Rep Power
    0

    Default

    It depends of your needs, neither solution is bad applied to some scenarios, as you said iterable and iterator.

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by anahuacv View Post
    It depends of your needs, neither solution is bad applied to some scenarios, as you said iterable and iterator.
    I rest my case.

    kind regards,

    Jos

Similar Threads

  1. iterator issues
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-12-2008, 02:43 PM
  2. Iterator
    By eva in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 03:07 PM
  3. implementing shape
    By sidkdbl07 in forum Java 2D
    Replies: 1
    Last Post: 01-12-2008, 07:42 PM
  4. using Iterator with Vector
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-13-2007, 11:52 AM
  5. How to use an Iterator in java
    By lenny in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 08:46 PM

Posting Permissions

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