码迷,mamicode.com
首页 > 编程语言 > 详细

java 栈

时间:2019-10-19 20:57:07      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:int   pre   one   ring   static   oid   display   str   public   

 

package testjavapro;
import java.util.*;

public class testjava {
 
    static void showpush(Stack<Integer> st, int a) {
        st.push(new Integer(a));
        System.out.println("push(" + a + ")");
        System.out.println("cur stack: " + st);
    }
 
    static void showpop(Stack<Integer> st) {
        System.out.print("pop -> ");
        Integer a = (Integer) st.pop();
        System.out.println(a);
        System.out.println("cur 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");
        }
    }
}

输出

stack: []
push(42)
cur stack: [42]
push(66)
cur stack: [42, 66]
push(99)
cur stack: [42, 66, 99]
pop -> 99
cur stack: [42, 66]
pop -> 66
cur stack: [42]
pop -> 42
cur stack: []
pop -> empty stack

 

 

 

 

java 栈

标签:int   pre   one   ring   static   oid   display   str   public   

原文地址:https://www.cnblogs.com/sea-stream/p/11704794.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!