如何在 Java 1.4 中向自定义类添加迭代器的示例?

发布于 2024-07-23 08:54:57 字数 366 浏览 6 评论 0原文

有人能给我一个在 Java 5 的可迭代接口之前向自定义类添加迭代器的最佳方法的示例吗?

包装 Collection 是最好的选择吗? 就像

public Iterator iterator() {
    return wrappedCollection.iterator();
}

在我最初的文章中我将 Iterator 与 Iterable 混淆了。 我感兴趣的最终结果是能够使用以下内容对自定义类进行操作。

Iterator it = customClass.iterator();
while (it.hasNext()) {
    //do stuff
}

Could someone give me an example of the best way to add an iterator to a custom class pre Java 5's iterable interface?

Would wrapping a Collection be the best option? Something like

public Iterator iterator() {
    return wrappedCollection.iterator();
}

In my initial post I confused Iterator with Iterable. The end result I am interested in is being able to operate on the custom class with the following.

Iterator it = customClass.iterator();
while (it.hasNext()) {
    //do stuff
}

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

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

发布评论

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

评论(6

可爱暴击 2024-07-30 08:54:57

如果您只是包装集合,则可以使用转发方法。

public class Custom implements Collection {
    Collection c; // create an instance here or in the constructor
    ...

    // forwarding method
    public Iterator iterator()
    {
        return c.iterator();
    }
}

我认为最好实现您正在包装的任何类型的集合接口,而不是迭代器。

If you're just wrapping a collection you can use a forwarding method.

public class Custom implements Collection {
    Collection c; // create an instance here or in the constructor
    ...

    // forwarding method
    public Iterator iterator()
    {
        return c.iterator();
    }
}

I think it would be better to implement whatever type of Collection Interface you're wrapping, though, instead of Iterator.

ゞ花落谁相伴 2024-07-30 08:54:57

您是否添加了类似的方法

public Iterator iterator() {
    return new YourCustomIterator(...);
}

Did you add a method that looks like

public Iterator iterator() {
    return new YourCustomIterator(...);
}

?

岁月静好 2024-07-30 08:54:57

听起来您刚刚将那些方法添加到包含集合的类中。 在这种情况下,您的对象现在也是您的迭代器。 您可能想要做的是创建一个实现 Iterator 的新类,并通过 iterator() 方法使用 Collection 的克隆进行实例化。

It sounds like you just added those methods your class that contains the Collection. In that case, your object is now also your iterator. What you probably meant to do is create a new class that implements Iterator and is instantiated with a clone of your Collection via an iterator() method.

素食主义者 2024-07-30 08:54:57

如果您只想公开内部集合的迭代器,则只需返回该集合迭代器即可。 如果您想做的不止这些,您要么必须包装自己的组件,要么查看 Apache Commons Collections 包,其中包含相当多的迭代器生成器和修饰符方法。 (即使您无法使用他们的方法,他们的实现也应该教会您很多有关构建迭代器的知识。)

If you just want to expose the iterator of an internal collection you just return that collections iterator. If you want to do more than that, you either have to wrap your own or check out the Apache Commons Collections package which includes quite a few iterator bulider and modifier methods. (Even if you can't use their methods, their implementations should teach you a lot about building iterators.)

爱*していゐ 2024-07-30 08:54:57

您希望集合具有一个迭代器方法,该方法根据需要返回私有迭代器类的实例。

public class IterableTest
{
    public Iterator iterator()
    {
        return new IteratorTest();
    }
    private class IteratorTest implements Iterator
    {
        public boolean hasNext(){...}

        public Object next(){...}

        public void remove(){...}
    }
}

You want the collection to have an iterator method that returns an instance of a private Iterator class as needed.

public class IterableTest
{
    public Iterator iterator()
    {
        return new IteratorTest();
    }
    private class IteratorTest implements Iterator
    {
        public boolean hasNext(){...}

        public Object next(){...}

        public void remove(){...}
    }
}
飘然心甜 2024-07-30 08:54:57

我想您正在寻找类似下面的内容

http://karephul.blogspot.com/2009 /05/concurrentmodificationexception.html

& 是的,内部类的想法就是您正在寻找的。

I guess you are looking for something like below

http://karephul.blogspot.com/2009/05/concurrentmodificationexception.html

& yes, idea of inner class is what you are looking for.

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