title: I need clarification on how to add and remove elements from a StackArray
body: <p>I am implementing a StackArray and this is the first time I have done it. The program is supposed to push 5 elements onto the stack, The output I am looking for is [a,b,c,d,e], then it will pop the head of the list and print it again until the list is empty. However, my output is [a,b,c,d]. It completely ignores the last element. I believe my push and pop methods need to be modified just a bit. </p>
  
  <pre><code>import java.util.NoSuchElementException;
  
  public class StackArray //implements Stack interface
  { 
     private Object[] item; // The array where elements are stored
     private int top = 0;  // The index of the first empty location in the stack
     private int size = 2;  // The current number of item locations in the stack
     private Object[] temp = new Object[size];
     /**
        Constructs an empty stack.
     */
     public StackArray()
     {
        item = new Object[size];
     }
  
     public void push(Object element)
     {
        if (top == item.length)
        {
           size = item.length * 2;
           Object[] newItem = new Object[size];
           for (int i = 0; i &lt; item.length; i++) 
           { 
              newItem[i] = item[i]; 
           }      
           item = newItem;
        }
        item[top++] = element;               
     }      
     public Object pop()
     {
        if (isEmpty())
        {
           throw new NoSuchElementException();
        }
        return item[--top];              
     }   
     public boolean isEmpty()
     {
        return top == 0;
     }
  
     public String toString()
     {
        if (top == 0) { return "[]"; }
        String temp = "[" + item[0];
        int i = 1;
        while (i &lt; top)
        {
           temp = temp + ", " + item[i];
           i = i + 1;
        }
        temp = temp + "]";
        return temp;
     } 
  }
  </code></pre>
  
  <p>Here is my runner program:</p>
  
  <pre><code>public class StackArrayRunner
  {
     public static void main(String[] args)
     {
        StackArray sa = new StackArray();
        sa.push("a");
        sa.push("b");
        sa.push("c");
        sa.push("d");
        sa.push("e");
        System.out.println(sa);
        System.out.println(sa.pop());
        System.out.println(sa);
        System.out.println(sa.pop());
        System.out.println(sa);
        System.out.println(sa.pop());
        System.out.println(sa);
        System.out.println(sa.pop());
        System.out.println(sa);
        System.out.println(sa.pop());
        System.out.println(sa);
        System.out.println(sa.pop());
     }
  }
  </code></pre>
  
  <p>The output should be:
  [a,b,c,d,e]
  e
  [a,b,c,d]
  d
  [a,b,c]
  c
  [a,b]
  b
  [a]
  a</p>
  
  <p>Thank you!
  Ok, quick question about exceptions. My output is right now but it throws the No such element exception at the end. Is this normal? Is that supposed to happen. Here is the new output:</p>
  
  <pre><code>[a, b, c, d, e]
  e
  [a, b, c, d]
  d
  [a, b, c]
  c
  [a, b]
  b
  [a]
  a
  []
  Exception in thread "main" java.util.NoSuchElementException
      at StackArray.pop(StackArray.java:35)
      at StackArrayRunner.main(StackArrayRunner.java:22)
  
   ----jGRASP wedge2: exit code for process is 1.
   ----jGRASP: operation complete.
  </code></pre>