如何为列表编写迭代器?

发布于 2024-10-01 06:07:42 字数 639 浏览 4 评论 0原文

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

我的伪代码:

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

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

恍梦境° 2024-10-08 06:07:42

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

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

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

我认为你应该只需要一个索引。但基本的想法是正确的。

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

有两种方法:

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

  2. MyIterator.remove()抛出一个UnsupportedOperationException。根据 Iterator API 规范,remove 方法是可选方法。

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

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

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.

不语却知心 2024-10-08 06:07:42
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;
    }

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

}
卷耳 2024-10-08 06:07:42

扩展 java.util.AbstractList 怎么样?毕竟,这就是 java.util (但不在 java.util.concurrent) 执行。

这样你只需要实现

  • get(int)
  • add(E) (如果您想让列表可变)
  • 和一些构造函数

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文