Java Stack 类

作者: 龍飝 | 来源:发表于2019-07-03 09:38 被阅读0次

    import java.util.*;

    public class StackDemo {

    static void showpush(Stack<Integer> st,int a) {
        st.push(new Integer(a));
        System.out.println("push(" + a + ")");
        System.out.println("stack: " + st);
    }
    
    static void showpop(Stack<Integer> st) {
        System.out.print("pop -> ");
        Integer a = (Integer) st.pop();
        System.out.println(a);
        System.out.println("stack: " + st);
    }
    
    public static void main(String[] args) {
        Stack<Integer> st = new Stack<Integer>();
        System.out.println("stack: " + st);
        showpush(st, 42);
        showpush(st, 66);
        showpush(st, 99);
        showpop(st);
        showpop(st);
        showpop(st);
        try {
            showpop(st);
        } catch (EmptyStackException e) {
            System.out.println("empty stack");
        }
    }
    

    }

    相关文章

      网友评论

        本文标题:Java Stack 类

        本文链接:https://www.haomeiwen.com/subject/ckbfhctx.html