简体   繁体   中英

How to write Iterator for a list?

I have a class implementing List interface and storing data in an array of Objects. Now I need to write Iterator method for my class. How to get started ? I thought about writing a subclass implementing Iterator interface. Object of the class will have parameters of current index and last index. At each call to next/hasNext those parameters will be modified. Is this approach correct ? But then there is a problem with remove() method, since it should allow to delete object of class calling my iterator. How to solve this ? Also what should happen in iterator() method of my main class ?

My pseudocode:

class MyCollection<T> implements List<T>{
    T[] tab;

    MyCollection(int len) {
        tab = (T[])new Object[len];
    }
    public Iterator iterator(){
    }
}

class MyIterator<T> implements Iterator {

    private int current;
    private int last;

    public void remove(){
    }

    public T next(){

    }

    public boolean hasNext(){

    }

}

I have a class implementing List interface and storing data in an array of Objects.

It looks like you are reimplementing ArrayList . Is there a good reason for doing this?

Object of the class will have parameters of current index and last index. At each call to next/hasNext those parameters will be modified. Is this approach correct ?

You should only need one index, I think. But the basic idea is correct.

But then there is a problem with remove() method, since it should allow to delete object of class calling my iterator. How to solve this ?

There are two approaches:

  1. Remove the element from the array and somehow arrange that the "hole" is filled. Either a) copy all elements to a new array of size tab.length - 1 , b) use System.arraycopy or equivalent to move the elements after the deleted element, or c) assign null to the slot and change the classes to skip over null elements. (The last is probably a really bad idea ...)

  2. Have MyIterator.remove() throw an UnsupportedOperationException . The remove method is an optional method according to the Iterator API spec.

Also what should happen in iterator() method of my main class ?

It should create and return an instance of the MyIterator class.

看一看java.util.ArrayList

class MyCollection<T> implements List<T>{
    T[] tab;

    MyCollection(int len) {
        tab = (T[])new Object[len];
    }
    public Iterator iterator(){
        return new MyIterator(tab);
    }
}

class MyIterator<T> implements Iterator {

    private int current = 0;
    private int last ;
    private T[] tab;

    public MyIterator(T[] tab){
       this.tab = tab;
    }

    public void remove(){
       throw UnsupportedException();
    }

    public T next(){
        current ++ ;
        return tab[current];
    }

    public boolean hasNext(){
        current == tab.length - 1;
    }

}

How about extending java.util.AbstractList ? After all that's what all sun List implementations in java.util (but not in java.util.concurrent ) do.

That way you only need to implement

  • get(int) and
  • add(E) (if you want to make the list mutable)
  • and some Constructors

You get all other methods (including iterator() ) for free.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM