返回迭代器是什么意思? Java的 [英] What does it mean to return an iterator? Java

查看:142
本文介绍了返回迭代器是什么意思? Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须编写一个实现Iterable接口的类。我对返回迭代器对象的含义感到困惑。迭代器只是遍历列表的元素,那么我如何将其作为对象返回?我会返回一个能够迭代的列表吗?当迭代器完成所有操作时,如何通过或更改其他对象中的数据?

I have to write a class that implements the Iterable interface. I'm confused about what it means to return an iterator object. An iterator just goes through the elements of a list, so how would I return this as an object? Would I return a list that was able to be iterated through or what? How can an iterator be an object when all it does is go through or change data in other objects?

推荐答案

这是一个例子一个非常简单的清单。它将列表表示为链接元素。
迭代器对象创建为一个匿名内部类,将当前元素保存为状态。每次调用 iterator()都会创建一个新的迭代器对象。

Here is an example of a very simplistic list. It represents the list as linked elements. The iterator object is created as an anonymous inner class holding the current element as the state. Each call of iterator() creates a new iterator object.

import java.util.Iterator;

public class SimplisticList<T> implements Iterable<T> {

  /*
   * A list element encapsulates a data value and a reference to the next
   * element.
   */
  private static class Element<T> {
    private T data;
    private Element<T> next;

    Element(T data) {
      this.data = data;
      next = null;
    }

    public T getData() {
      return data;
    }

    public Element<T> getNext() {
      return next;
    }

    public void setNext(Element<T> next) {
      this.next = next;
    }

  }

  // We only need a reference to the head of the list.
  private Element<T> first = null;

  // The list is empty if there is no first element.
  public boolean isEmpty() {
    return first == null;
  }

  // Adding a new list element.
  // For an empty list we only have to set the head.
  // Otherwise we have to find the last element to add the new element.
  public void add(T data) {
    if(isEmpty()) {
      first = new Element<T>(data);
    } else {
      Element<T> current = first;
      while(current.getNext() != null) {
        current = current.getNext();
      }
      current.setNext(new Element<T>(data));
    }
  }

  @Override
  public Iterator<T> iterator() {
    // Create an anonymous implementation of Iterator<T>.
    // We need to store the current list element and initialize it with the
    // head of the list.
    // We don't implement the remove() method here. 
    return new Iterator<T>() {
      private Element<T> current = first;

      @Override
      public boolean hasNext() {
        return current != null;
      }

      @Override
      public T next() {
        T result = null;
        if(current != null) {
          result = current.getData();
          current = current.getNext();
        }
        return result;
      }

      @Override
      public void remove() {
        // To be done ...
        throw new UnsupportedOperationException();
      }
    };
  }

}

这篇关于返回迭代器是什么意思? Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆