在同一个类中实现 Java Iterator 和 Iterable?
我正在尝试理解 Java Iterator
和 Iterable
接口,
我正在编写这个类,
class MyClass implements Iterable<String> {
public String[] a = null;
public MyClass(String[] arr) {
a = arr;
}
public MyClassIterator iterator() {
return new MyClassIterator(this);
}
public class MyClassIterator implements Iterator<String> {
private MyClass myclass = null;
private int count = 0;
public MyClassIterator(MyClass m) {
myclass = m;
}
public boolean hasNext() {
return count < myclass.a.length;
}
public String next() {
int t = count;
count++;
return myclass.a[t];
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}
它似乎正在工作。
我应该:
Myclass implements Iterable<Stirng>, Iterator<String> {
}
或者我应该将 MyClassIterator
放在 MyClass
之外,因为
class MyClass implements Iterable<String> {
public String[] a = null;
public MyClass(String[] arr) {
a = arr;
}
public MyClassIterator iterator() {
return new MyClassIterator(this);
}
}
public class MyClassIterator implements Iterator<String> {
private MyClass myclass = null;
private int count = 0;
public MyClassIterator(MyClass m) {
myclass = m;
}
public boolean hasNext() {
return count < myclass.a.length;
}
public String next() {
int t = count;
count++;
return myclass.a[t];
}
public void remove() {
throw new UnsupportedOperationException();
}
}
哪个更好?
I am trying to understand Java Iterator
and Iterable
interfaces
I am writing this class
class MyClass implements Iterable<String> {
public String[] a = null;
public MyClass(String[] arr) {
a = arr;
}
public MyClassIterator iterator() {
return new MyClassIterator(this);
}
public class MyClassIterator implements Iterator<String> {
private MyClass myclass = null;
private int count = 0;
public MyClassIterator(MyClass m) {
myclass = m;
}
public boolean hasNext() {
return count < myclass.a.length;
}
public String next() {
int t = count;
count++;
return myclass.a[t];
}
public void remove() {
throw new UnsupportedOperationException();
}
}
}
It seems to be working.
Should I have:
Myclass implements Iterable<Stirng>, Iterator<String> {
}
Or I should put MyClassIterator
outside MyClass
as
class MyClass implements Iterable<String> {
public String[] a = null;
public MyClass(String[] arr) {
a = arr;
}
public MyClassIterator iterator() {
return new MyClassIterator(this);
}
}
public class MyClassIterator implements Iterator<String> {
private MyClass myclass = null;
private int count = 0;
public MyClassIterator(MyClass m) {
myclass = m;
}
public boolean hasNext() {
return count < myclass.a.length;
}
public String next() {
int t = count;
count++;
return myclass.a[t];
}
public void remove() {
throw new UnsupportedOperationException();
}
}
Which one is better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您几乎永远不应该在同一个类中同时实现
Iterable
和Iterator
。他们做不同的事情。迭代器自然是有状态的 - 当您迭代使用它时,它必须更新其世界观。然而,可迭代对象只需要能够创建新的迭代器。特别是,您可以让多个迭代器同时处理同一个原始可迭代对象。您当前的方法非常好 - 我会更改实现的某些方面,但在职责分离方面很好。
You should almost never implement both
Iterable
andIterator
in the same class. They do different things. An iterator is naturally stateful - as you iterate using it, it has to update its view of the world. An iterable, however, only needs to be able to create new iterators. In particular, you could have several iterators working over the same original iterable at the same time.Your current approach is pretty much okay - there are aspects of the implementation I'd change, but it's fine in terms of the separation of responsibilities.
您的第一次尝试就已步入正轨。
MyClass
只需要实现Iterable
,而这又需要您提供Iterator
实现来从返回可迭代.iterator()
。无需将
MyClassIterator
放在MyClass
之外,因为在大多数情况下,您甚至不需要直接使用Iterator
(它是由Iterable上的
for .. in ..
语法隐式使用),并且在所有其他情况下,接口就足够了,除非您实际上向实施(您可能永远不需要这样做)。这是我的做法,请参阅内嵌的评论:
You were on track with your first try.
MyClass
only needs to implementIterable<String>
, which in turn requires you to provide anIterator<String>
implementation to return fromIterable<String>.iterator()
.There's no need to put the
MyClassIterator
outside ofMyClass
because in most cases you will never even need to directly use theIterator<String>
(it's used implicitly by thefor .. in ..
syntax onIterable<String>
s), and in all other cases the interface is sufficient unless you actually add additional behavior to the implementation (which you likely won't ever need to do).Here's how I'd do it, see comments inlined:
您不应该执行
Myclass Implements Iterable,Iterator{
因为迭代器是一次性的。除了列表迭代器之外,没有办法将它们返回到开头。顺便说一句,您可以跳过
and 而不是引用引用
您
的内部类不是静态的,因此 MyClass.this 将引用创建它的封闭类的实例。
You should not do
Myclass implements Iterable<String>,Iterator<String>{
since iterators are single-use. With the exception of list iterators, there's no way to return them to the start.Incidentally, you can skip the
and instead of referencing
reference
Your inner class is not static, so
MyClass.this
will reference the instance of the enclosing class that created it.