适配器设计模式|将 Iterator 转换为 Enumeration 的适配器 |ConcurrentModificationException
作为练习的一部分,我将实现一个支持枚举的 ArrayList。
以下是将迭代器转换为枚举的适配器:
public class MyEnumeratorAdapter<Object> implements Enumeration<Object> {
private Iterator<Object> adaptee;
public MyEnumeratorAdapter(Iterator<Object> it) {
this.adaptee = it;
}
@Override
public boolean hasMoreElements() {
return adaptee.hasNext();
}
@Override
public Object nextElement() {
return adaptee.next();
}
}
我的 arraylist 类是:
public class MyArrayList<Object> extends ArrayList<Object> {
public MyArrayList() {
this.enumerator = new MyEnumeratorAdapter<Object>(this.iterator());
}
public Enumeration<Object> enumerator() {
return this.enumerator;
}
public boolean hasMoreElements() {
return this.enumerator.hasMoreElements();
}
public Object nextElement() {
return this.enumerator.nextElement();
}
private static final long serialVersionUID = 1L;
private Enumeration<Object> enumerator;
}
但是,当我使用以下代码尝试进行此测试时,我收到 java.util.ConcurrentModificationException
public static void main(String[] args) {
MyArrayList<String> names = new MyArrayList<String>();
names.add("jim");
names.add("jack");
names.add("jai");
for (Enumeration<String> iterator = names.enumerator(); iterator
.hasMoreElements();) {
String name = (String) iterator.nextElement();
System.out.println(name);
}
}
我犯了什么错误正在做?
我可以有一个支持枚举的 ArrayList 类吗?
As part of an exercise, am implementing an ArrayList which will support Enumerations.
Following is the adapter that converts an Iterator to an Enumeration:
public class MyEnumeratorAdapter<Object> implements Enumeration<Object> {
private Iterator<Object> adaptee;
public MyEnumeratorAdapter(Iterator<Object> it) {
this.adaptee = it;
}
@Override
public boolean hasMoreElements() {
return adaptee.hasNext();
}
@Override
public Object nextElement() {
return adaptee.next();
}
}
and my arraylist class is:
public class MyArrayList<Object> extends ArrayList<Object> {
public MyArrayList() {
this.enumerator = new MyEnumeratorAdapter<Object>(this.iterator());
}
public Enumeration<Object> enumerator() {
return this.enumerator;
}
public boolean hasMoreElements() {
return this.enumerator.hasMoreElements();
}
public Object nextElement() {
return this.enumerator.nextElement();
}
private static final long serialVersionUID = 1L;
private Enumeration<Object> enumerator;
}
however, when I try this testing with the following code, I'm getting java.util.ConcurrentModificationException
public static void main(String[] args) {
MyArrayList<String> names = new MyArrayList<String>();
names.add("jim");
names.add("jack");
names.add("jai");
for (Enumeration<String> iterator = names.enumerator(); iterator
.hasMoreElements();) {
String name = (String) iterator.nextElement();
System.out.println(name);
}
}
what mistake am I doing?
Can I have an ArrayList class that support Enumerations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MyArrayList
有几个问题:Iterator
;每次调用枚举器时都必须创建一个新的迭代器应该解决这些问题:
MyArrayList
has several issues:Iterator
on a zero sized array list; you must create a fresh iterator each time you call enumeratorMyArrayList
This class should fix these issues:
您最终会重复使用相同的
Iterator
。您每次都需要一个新的(通过每次创建一个新的枚举
)。You end up using the same
Iterator
repeatedly. You need a new one each time (by creating a newEnumeration
each time).