What does it mean to return an iterator? Java

I need to write a class that implements the Iterable interface. I am confused about what it means to return an iterator object. The iterator just loops through the elements of the list, so how can I return this as an object? Would I return a repeatable list or what? How can an iterator be an object, when all this is happening is to move or modify data in other objects?

+3


source to share


3 answers


Storing an iterator means returning an instance of a class that implements the Iterator interface. This class must implement hasNext()

, next()

and remove()

. The class constructor must initialize the instance so that it next()

returns the first element of the data structure you are iterating over (if it is not empty).



+2


source


Here's an example of a very simplified list. It presents the list as related items. The iterator object is instantiated as an anonymous inner class that holds the current element as state. Each call 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();
      }
    };
  }

}

      

+2


source


Here's a simple example of an iterator that loops through an array String[]

:

public class MyIterator implements Iterator<String> {

    private String[] arr;
    private int index;

    public MyIterator(String[] arr) {
        this.arr = arr;
        this.index = 0;
    }

    public boolean hasNext() {
        return index < arr.length;
    }

    public String next() {
        return arr[index++];
    }

}

      

(You also need to remove()

, but that will just throw an exception). Note that when you construct one of these iterators with, new MyIterator(myStringArray)

you are creating an object with an array reference . Iterator

will not be the array itself or any part of it, but has a private variable that refers to it. Iterator

for a list or for any other data structure (or even things that are not data structures) will follow a similar pattern.

+1


source







All Articles