title: Implementing an iterator for a stack
body: <p>I defined an interface <strong>StringStack</strong></p>
  
  <pre><code>public interface StringStack{
      //add a value to the stack
      public void push(String value);
      //fetch top-most element of the stack. element is removed
      public String pop();
      //fetch top-most element of the stack. element is not removed
      public String peek();
  }
  </code></pre>
  
  <p>Further I defined a class <strong>SimpleStack</strong> that uses an <strong>ArrayList</strong> to manage the stack</p>
  
  <pre><code>public class SimpleStack implements StringStack{
      private ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
  
      public void push(String value){
          list.add(value);
      }
  
      public String pop(){
          if(!list.isEmpty()){
                  return list.remove(list.size() - 1);
          }else{
                  return null;
          }
      }
  
     public String peek(){
          if(!list.isEmpty()){
                  return list.get(list.size() - 1);
          }else{
                  return null;
          }
  }
  </code></pre>
  
  <p>Now I want define an iterator for my stack class but I don't want to use the built-in ArrayList iterator. So I came up implementing a inner class and extending my SimpleStack with the <strong>Iterable</strong> interface.</p>
  
  <p>So I have now:</p>
  
  <pre><code> public class SimpleStack implements StringStack, Iterable&lt;String&gt;
  
   ...
  
   public Iterator&lt;String&gt; iterator(){
      return new StackEnum();
   }
  
   class StackEnum implements Iterator&lt;String&gt;{
  
      int pos = list.size();
  
      public boolean hasNext(){
          return pos != 0;
      }
  
      public String next(){
          if(pos != 0){
              String str = list.get(pos);
              pos--;
          }else{
              throw new NoSuchElementException();
          }
      }
  
      public void remove(){
          throw new UnsupportedOperationException();
      }
   }
  </code></pre>
  
  <p>I am absolutely not sure how to perform the iteration inside the iterator. Since my stack is represented by an array list I used list.size() as top-element.</p>
  
  <p>Am I right with my implementation of the iterator, especially the next method?</p>