java - 实现 ArrayList 的迭代器
问题描述
我在向 ArrayList 添加迭代器支持的代码时遇到问题,这是我为实现 Iterator 而创建的类
class MyArrayListIterator<E> implements Iterator<E> {
private E[] list = null;
private int currentIndex = 0;
@Override
public boolean hasNext() {
if (currentIndex < list.length) {
return true;
} else {
return false;
}
}
@Override
public E next(){
return list[currentIndex++];
}
这必须包括,我认为我做对了
MyArrayList 类型的“list”
int 类型的“currentIndex”,最初为零
这是我的主要测试方法
public static void main(String[] args) throws Exception {
MyList<String> names = new MyArrayList<>();
names.add("Steve");
names.add("Frank");
names.add("Heather");
names.add("Chris");
names.add("Oliver");
for (String string : names) { // error at names Can only iterate over an array or an instance of java.lang.Iterable
System.out.println(string);
}
}
}
在我添加的 myArrayList 中,要求是通过添加 iterator() 方法使 MyArrayList 实现 Iterable 接口,该方法应返回 MyArrayListIterator 的实例。
public Iterator<E> iterator() {
return new MyArrayListIterator();
}
请让我知道我做错了什么。
解决方案
正如评论中已经提到的,您的问题是您在没有初始化的情况下从list
in 中读取。MyArrayListIterator
这会导致NullPointerException
.
您可以通过两种不同的方式解决此问题:
创建
MyArrayListIterator
一个non-static nested
(它们也被称为inner classes
)类MyArrayList
。通过这样做,您可以访问外部类的所有字段,在本例中为
MyArrayList
. 例如,请参阅以下代码片段:
public class MyArrayList<E> implements MyList<E> {
private Object[] list = new Object[10];
private int size = 0;
public Iterator<E> iterator(){
return new MyArrayListIterator<>();
}
// more of your implementation...
// This is the inner class
private class MyArrayListIterator<T> implements Iterator<T> {
private int currentIndex = 0;
@Override
public boolean hasNext() {
// Since this is a inner class, we have access to the
// "list" field defined by MyArrayList.
return currentIndex < list.length;
}
@Override
public T next() {
// Since this is a inner class, we have access to the
// "list" field defined by MyArrayList.
return (T) list[currentIndex++];
}
}
}
创建
MyArrayListIterator
一个static nested
班级或单独的班级。在这种情况下,您无权访问 中定义的字段
MyArrayList
,这意味着您必须自己提供它们。这可以使用构造函数来完成。
public class MyArrayList<E> implements MyList<E> {
private Object[] list = new Object[10];
private int size = 0;
public Iterator<E> iterator(){
return new MyArrayListIterator<>((E[])list);
}
// More of your list implementation
// This is a static inner class, which means we do not have access to
// the fields of the outer class.
private static class MyArrayListIterator<T> implements Iterator<T> {
private final T[] elements;
private int currentIndex = 0;
public MyArrayListIterator(T[] elements) {
this.elements = elements;
}
@Override
public boolean hasNext() {
// Since we got the elements as constructor argument, they are not
// null (assuming you did not call it with null as parameter).
return currentIndex < elements.length;
}
@Override
public T next() {
// Since we got the elements as constructor argument, they are not
// null (assuming you did not call it with null as parameter).
return elements[currentIndex++];
}
}
}
有关嵌套类的更多信息,请参阅Oracle的关于嵌套类的教程。
推荐阅读
- angular - Angular 4-错误-找不到“对象”类型的不同支持对象“[对象对象]”
- c++ - 从 const char*、char* 参数到 std::string 的转换接口
- excel - 在 Word 和 Excel 之间插入链接
- php - 将 XML 转换为 JSON 并保存到磁盘后缺少根标记
- azure - 如果 vm 的磁盘是高级或标准,则使用 get-azurermvm 获取存储类型
- c# - AMSI:按块对大文件执行恶意软件扫描
- python - python完成,文件与对象
- adobe - 根据订单项字段隐藏 Adobe Form 中的页脚
- java - java中的UTF-8字符串解码,平假名符号变得不可读
- php - PHPMailer 不发送邮件