title: Is there a bug in java.util.Stack's Iterator?
body: <p>Today I was trying to push in <code>java.util.Stack</code> class and then use the <code>Iterator</code> to iterate (without using pop) through the items. I was expecting LIFO property but got surprised.</p>
  
  <p>Here is the code that I was trying.</p>
  
  <pre><code>import java.util.*;
  import java.util.Stack;
  
  public class Main {
      public static void main(String[] args) {
          RobStack&lt;Integer&gt; rstack = new RobStack&lt;Integer&gt;(); // Correct Implementation
          Stack&lt;Integer&gt; jstack = new Stack&lt;Integer&gt;(); // Default Java Implementation
          rstack.push(0); jstack.push(0);
          rstack.push(1); jstack.push(1);
          rstack.push(2); jstack.push(2);
          rstack.push(3); jstack.push(3);
  
          System.out.print("Algo Stack: ");
          for (int i : rstack)
              System.out.print(i + " ");
          System.out.print("\nJava Stack: ");
          for (int i : jstack)
              System.out.print(i + " ");
      }
  
  }
  </code></pre>
  
  <p>The output the above program is given below:</p>
  
  <pre><code>Algo Stack: 3 2 1 0 
  Java Stack: 0 1 2 3 
  </code></pre>
  
  <p>In the above code <code>jstack</code> uses the default Java implementation and <code>rstack</code> uses the <a href="http://algs4.cs.princeton.edu/13stacks/Stack.java" rel="noreferrer">implementation provided by Robert Sedgewick</a> for his Algorithm class. I found that Prof. Robert's implementation works fine but the <code>java.util.Stack</code> implementation fails. </p>
  
  <p>Is it a <strong>bug</strong> or is it by <strong>design</strong>?</p>