首页 > 解决方案 > 实现 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();
}

请让我知道我做错了什么。

标签: javaiterator

解决方案


正如评论中已经提到的,您的问题是您在没有初始化的情况下从listin 中读取。MyArrayListIterator这会导致NullPointerException.

您可以通过两种不同的方式解决此问题:

  1. 创建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++];
    }
  }
}
  1. 创建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的关于嵌套类的教程。


推荐阅读