如何为列表编写迭代器?
我有一个实现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来您正在重新实现 ArrayList。这样做有充分的理由吗?
我认为你应该只需要一个索引。但基本的想法是正确的。
有两种方法:
从数组中删除元素并以某种方式安排“洞”被填充。 a) 将所有元素复制到大小为
tab.length - 1
的新数组,b) 使用System.arraycopy
或等效方法将元素移动到已删除元素之后,或者c) 将 null 分配给槽并更改类以跳过null
元素。 (最后一个可能是一个非常糟糕的主意......)让
MyIterator.remove()
抛出一个UnsupportedOperationException
。根据Iterator
API 规范,remove
方法是可选方法。它应该创建并返回 MyIterator 类的实例。
It looks like you are reimplementing
ArrayList
. Is there a good reason for doing this?You should only need one index, I think. But the basic idea is correct.
There are two approaches:
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) useSystem.arraycopy
or equivalent to move the elements after the deleted element, or c) assign null to the slot and change the classes to skip overnull
elements. (The last is probably a really bad idea ...)Have
MyIterator.remove()
throw anUnsupportedOperationException
. Theremove
method is an optional method according to theIterator
API spec.It should create and return an instance of the MyIterator class.
看一下 java.util.ArrayList。
Take a look at java.util.ArrayList.
扩展 java.util.AbstractList 怎么样?毕竟,这就是
java.util
(但不在java.util.concurrent
) 执行。这样你只需要实现
您将获得所有其他方法(包括 iterator())免费。
How about extending java.util.AbstractList? After all that's what all sun List implementations in
java.util
(but not injava.util.concurrent
) do.That way you only need to implement
You get all other methods (including iterator()) for free.