title: printing stack without popping elements java
body: <p>for a task I have to write a method which prints the stack, that part is easy</p>
  
  <pre><code>public void print(stack s)
  {
     while(!isEmpty())
     {
        System.out.println(s.peek());
        s.pop();
     }
  
  }
  </code></pre>
  
  <p>The problem is that after I printed the stack, my task is to print the bottom element on the stack, which isn't there any more cause I used s.pop() in my print method. This is my code for printing the bottom element.</p>
  
  <pre><code>public void bottom(stack s)
  {
    if(isEmpty())
     {
       System.out.println("Stack is empty");
     }
    else
     {
       System.out.println(stackArray[0]);
     }
  }
  </code></pre>
  
  <p>My question is: How should i modifie the print method so I don't have to pop the elements from the stack? Or is there another way to make it so that the stack still holds my elements after using print method?</p>
  
  <p>as resquested this is the stack we're using in our classes(most of it is in dutch):</p>
  
  <pre><code>public class MyStack
  {
      protected Object[ ] stackArray;
      protected int top;
      private int grootte;
      private static final int DEFAULT_GROOTTE = 10;
  
      public MyStack( )
      {
          grootte = DEFAULT_GROOTTE;
          stackArray = new Object[grootte];
          top = 0;
      }
  
      public boolean isEmpty( )
      {
          if (top == 0)
                  return true;
              else 
                  return false;
      }
  
      public void push(Object e)
      {
          if (top == grootte)
              allocateMore( );
          stackArray[top] = e;
          top++;
      }
  
      public Object pop( )
      {
              if(isEmpty( ))
              {
                  System.out.println("Stack leeg : er kan geen element van de stack afgehaald worden.");
                  return null;
              }
                      top--;
                      return stackArray[top];
  
  }
      public Object peek( )
      {
              if(isEmpty( ))
              {
                  System.out.println("Stack leeg : er kan geen topelement van de stack getoond worden.");
                  return null;
              }
                      return stackArray[top-1];
      }
  
      public int size( )
      {
          return top;
      }
  
      private void allocateMore( )
      {
          Object[ ] original = stackArray;
          grootte = grootte * 2;
          stackArray = new Object[ grootte];
          for(int i = 0; i &lt; grootte/2; i++)
          {
              stackArray[i] = original[i];
      }
      }
  
  }
  </code></pre>
  
  <p>since my rep isn't high enough to answer my own question a quick edit</p>
  
  <p>I think I've found an other way to print the stack using this</p>
  
  <pre><code>public void print(stack s)
  {
   for(int i =top-1; i&gt;=0;i--)
     System.out.println(stackArray[i]);
  }
  </code></pre>
  
  <p>it probably isn't the best way to do it, but it's working :P</p>