title: Implementing Iterator on a Linked List
body: <p>I am creating a implementation of a LinkedList using just a few of the methods in the standard implementation. I have faced a problem when adding the Iterator functionality to my LinkedList. </p>
  
  <p>Right now I can add some elements, and then to something like this </p>
  
  <pre><code>class testList {
  
     public static void main(String[] args){
  
        MyLinkedList&lt;String&gt; list = new MyLinkedList&lt;String&gt;();
        list.add("Element 0");
        list.add("Element 1");
        list.add("Element 2");
  
        Iterator&lt;String&gt; iter = list.iterator();
  
        System.out.println(iter.next());
        System.out.println(iter.next());
        System.out.println(iter.next());
  
        for (String s : list){
          System.out.println(s);
        }
      }
    }
  </code></pre>
  
  <p>And everything works except the last part, when trying to compile it I get a error saying error: for-each not applicable to expression type for (String s : list)</p>
  
  <p>Is there something obvious I have overlooked in the implementation? </p>
  
  <p>Here is the rest of my code, I am using custom Interfaces for Iterable and List </p>
  
  <pre><code>interface List&lt;T&gt; extends Iterable&lt;T&gt; {
  public int size();
  public void add(int pos, T x);
  public void add(T x); 
  }
  
  interface Iterator&lt;T&gt; {
  boolean hasNext();
  T next();
  }
  
  interface Iterable&lt;T&gt; {
  Iterator&lt;T&gt; iterator();
  }
  </code></pre>
  
  <p>And here is the main part of the code</p>
  
  <pre><code>import java.util.* ;
  
  class MyLinkedList&lt;T&gt; implements List&lt;T&gt; {
  
    private Node&lt;T&gt; head;
    private Node&lt;T&gt; tail;
    private int currentSize;
  
    //constructor for class
    public MyLinkedList(){
      this.head = null;
      this.tail = null;
      this.currentSize = 0;
    }
  
    //Node class used to hold the information, and link to each other
    public class Node&lt;E&gt; {
        private E data;
        private Node&lt;E&gt; next;
  
        //constructor for node class
        public Node(E data, Node&lt;E&gt; next){
          this.data = data;
          this.next = next;
        }
        public E getData(){
          return this.data;
        }
        public void setData(E newData){
          this.data = newData;
        }
        public Node&lt;E&gt; getNext(){
          return this.next;
        }
        public void setNext(Node&lt;E&gt; newNext){
          this.next = newNext;
        }
    }
  
    class LinkedListIterator implements Iterator&lt;T&gt;{
      private Node&lt;T&gt; current;
      public LinkedListIterator(){
        current = head;
      }
      public T next(){
        if (current == null){
          throw new NoSuchElementException();
        }
        T temp = current.getData();
        current = current.getNext();
        return temp;
      }
      public boolean hasNext(){
        return current != null;
      }
    }
    public Iterator&lt;T&gt; iterator(){
      return new LinkedListIterator();
    }
    public int size(){
      return this.currentSize;
    }
    public boolean isEmpty(){
      return this.size() == 0;
    }
    public void add(int pos, T x){
      if(pos &lt; 0 || pos &gt; size()){
        throw new IndexOutOfBoundsException();
      }
      if(pos == size()){
        add(x);
        return;
      }
      if (pos == 0){
        head = new Node(x, head);
      }else{
        Node&lt;T&gt; current = head;
        for(int j = 0; j &lt; pos-1; j++){
          current = current.getNext();
        }
        current.setNext(new Node(x, current.getNext()));
      }
      currentSize++;
  
    }
    public void add(T x){
      if(isEmpty()){
        head = new Node(x, null);
        tail = head;
      }else{
        tail.setNext(new Node(x, null));
        tail = tail.getNext();
      }
      currentSize++;
    }
  
  }
  </code></pre>