Stack: pop() : Stack « java.util « Java by API






Stack: pop()

 
/*
 * Output:
 * 
stack: []
push(42)
stack: [42]
pop -> 42
stack: []
empty stack


 *  
 */

import java.util.Stack;
import java.util.EmptyStackException;

public class MainClass {

  public static void main(String args[]) {
    Stack st = new Stack();
    System.out.println("stack: " + st);
    st.push(new Integer(42));
    System.out.println("push(" + 42 + ")");
    System.out.println("stack: " + st);

    System.out.print("pop -> ");
    Integer a = (Integer) st.pop();
    System.out.println(a);
    System.out.println("stack: " + st);

    try {
      st.pop();
    } catch (EmptyStackException e) {
      System.out.println("empty stack");
    }
  }
}
           
         
  








Related examples in the same category

1.new Stack()
2.new Stack < E > ()
3.Stack: empty()
4.Stack: peek()
5.Stack: push(E item)
6.Stack: search(Object o)