java 如何为列表编写迭代器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4121414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:51:45  来源:igfitidea点击:

How to write Iterator for a list?

javacollectionsiterator

提问by mastodon

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 ?

我有一个类实现 List 接口并将数据存储在对象数组中。现在我需要为我的类编写 Iterator 方法。如何开始?我想写一个实现 Iterator 接口的子类。该类的对象将具有当前索引和最后一个索引的参数。在每次调用 next/hasNext 时,这些参数将被修改。这种方法正确吗?但是 remove() 方法存在问题,因为它应该允许删除调用我的迭代器的类的对象。如何解决这个问题?在我的主类的 iterator() 方法中应该发生什么?

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(){

    }

}

回答by Stephen C

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

我有一个类实现 List 接口并将数据存储在对象数组中。

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

看起来您正在重新实现ArrayList. 这样做有充分的理由吗?

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 ?

该类的对象将具有当前索引和最后一个索引的参数。在每次调用 next/hasNext 时,这些参数将被修改。这种方法正确吗?

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 ?

但是 remove() 方法存在问题,因为它应该允许删除调用我的迭代器的类的对象。如何解决这个问题?

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.arraycopyor equivalent to move the elements after the deleted element, or c) assign null to the slot and change the classes to skip over nullelements. (The last is probably a really bad idea ...)

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

  1. 从数组中删除元素并以某种方式安排填充“洞”。a) 将所有元素复制到大小为 的新数组tab.length - 1,b) 使用System.arraycopy或等效于在删除的元素之后移动元素,或 c) 将 null 分配给插槽并更改类以跳过null元素。(最后一个可能是一个非常糟糕的主意......)

  2. MyIterator.remove()抛出UnsupportedOperationExceptionremove根据IteratorAPI 规范,该方法是可选方法。

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

在我的主类的 iterator() 方法中应该发生什么?

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

它应该创建并返回 MyIterator 类的一个实例。

回答by TofuBeer

回答by u443966

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

}

回答by Sean Patrick Floyd

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.

扩展java.util.AbstractList怎么样?毕竟这就是java.util(但不是java.util.concurrent)中的所有 sun List 实现。

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.

您可以免费获得所有其他方法(包括iterator())。