Java 类 Stack 使用方法

http://blog.sina.com.cn/s/blog_622bd1660100qb95.html

 

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);
   
   st.push(new Integer(43));
   System.out.println("push(" + 43 + ")");
   System.out.println("stack: " + st);
   
   System.out.print("pop -> ");
   Integer a = (Integer) st.pop();
   System.out.println(a);
   System.out.println("stack: " + st);
   
   Integer a1 = (Integer) st.pop();
   System.out.println(a1);
   System.out.println("stack: " + st);

   try {//错误处理
     st.pop();
   } catch (EmptyStackException e) {
     System.out.println("empty stack");
   }
 }
}

 

posted on 2013-03-12 19:28  Sure_Yi  阅读(280)  评论(0编辑  收藏  举报

导航