title: Iterator print LinkedList
body: <p>I have a problem regarding the display of my LinkedList with a foreach.</p>
  
  <p>When I want to display my LikedList an error comes from my list which tells me that I have to use an iterator. Here is the code:</p>
  
  <pre><code>public class Set {
  
  private LinkedList &lt;Integer&gt; elements;
  
  public Set () {
      this.elements = new LinkedList &lt;Integer&gt; ();
  }
  
  public static void main (String [] args) {
      Set test = new Set ();
      test.add (8);
      test.add (2);
      test.add (7);
      for (int e: test) {
  
      }
  }
  </code></pre>
  
  <p>So I used an iterator for the display but a message tells me that my iterator is undefined for the method set.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public class Set {
  
  private LinkedList &lt;Integer&gt; elements;
  
  public Set () {
      this.elements = new LinkedList &lt;Integer&gt; ();
  }
  
  public static void main (String [] args) {
      Set test = new Set ();
      test.add (8);
      test.add (2);
      test.add (7);
      Iterator &lt;Integer&gt; iterator = test.iterator ();
      while (iterator.hasNext ()) {
          System.out.println (iterator.next ());
      }
  }
  </code></pre>
  
  <p>If you can help me with that, it will help me a lot.</p>
  
  <p>(I tried to define "<code>Iterator &lt;Integer&gt; iterator = this.elements.iterator ();</code>" in the Set () public and call it in the hand that way but without success "<code>while (iterator.hasNext ()) {
  System.out.println (iterator.next ());
  }</code>") </p>
  
  <p>Thank you in advance, nice day</p>