创建自定义迭代器Java? [英] Creating custom Iterator Java?

查看:344
本文介绍了创建自定义迭代器Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何在Java中为类实现自定义迭代器感到困惑。我需要基本上创建一个ArrayList,而不使用我已经可用的内置库。我理解创建类的基础知识,但我无法理解如何让Iterator适应所有这些。我有以下内容:

I'm a bit confused about how to implement a custom iterator for a class in Java. I'm required to essentially make an ArrayList without using the inbuilt libraries already available to me. I understand the basics of creating the class but I'm having trouble understanding how to get the Iterator to fit into all of this. I have the following:

我创建了一个实现可迭代接口的泛型类,它看起来像这样:

I have created a generic class that implements the iterable interface as such it looks something like this:

public class MyArrayList<T> implements Iterable<T> {

然后我要创建一个名为MyIterator的类,根据文档的措辞是一个独立的课程。这看起来相当直接我创建了一个名为MyIterator的新类并让它实现了迭代器接口,所以它看起来像这样:

I've then got to create a class called MyIterator which according to the wording of the document is a stand alone class. This seems fairly straight forward I make a new class called MyIterator and have it implement the iterator interface so it looks something like this:

public class MyIterator<T> implements Iterator<T>{

我的困惑在于以下内容。该文档说Iterator需要在它自己的类中,但是如何访问MyArrayList中的数据成员以完全实现hasNext()和next()。由于底层数组中的数据成员是私有的(因为它们应该是),我没有看到外部类如何完全实现这些方法。我误解了需要什么?通过单独的类,它仍然是MyArrayList类的一部分,但定义不同吗?

My confusion lies in the following. The document says that the Iterator needs to be in it's own class, but how then do I access the data members in "MyArrayList" to fully implement hasNext() and next() for example. As the data members in the underlying array are private (as they should be) I don't see how an external class can fully implement these methods. Am I misunderstanding what is required? By separate class is it still a part of the "MyArrayList" class but defined differently?

我希望这有所帮助,正如我所说,我想我明白我需要什么我只是不确定我的迭代器适用于所有这些。

I hope that helps, as I said I think I understand what is required of me I just am not exactly sure where my Iterator fits into all of this.

推荐答案

虽然迭代器必须是一个单独的类< sup> * ,该类可能与您的 Iterable 类有一些关系。

While the iterator has to be a separate class *, that class will probably have some relation to your Iterable class.

它通常是一个嵌套/内部类,正是因为它需要访问类的值(这就是内部类的内容)。

It's often a nested/inner class, precisely because it needs to access the values of the class (and that's what pretty much what inner classes are made for).

当然,如果 Iterable 是一个列表你可以实现一个 Iterator 而不用任何完全内部访问,但你通常仍然希望访问内部,例如检查 modCount (抛出 ConcurrentModificationException Iterable 结构修改时当你迭代它时,如果你通过 Iterator 本身修改它,那么阻止该异常。

Granted, if the Iterable is a List you could implement an Iterator without any "internal" access at all, but you usually still want to get access to the internals for things like checking the modCount (to throw a ConcurrentModificationException when the Iterable is structurally modified while you iterate over it ... and to prevent that exception if you modify it via the Iterator itself).

*你可能用你的 Iterable 实例本身实现它,但这会破坏合同用户同时使用两个迭代器。

* you could implement it with your Iterable instance itself, but that would break the contract as soon as the user uses two iterators at the same time.

这篇关于创建自定义迭代器Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆