java中的栈Stack的基本使用和应用(一)

定义  栈是一种只能在一端进行插入或删除操作的线性表。(先进后出表)

java中的Stack继承Vector

实例化

Stack stack=new Stack();

基本使用

判断是否为空

stack.empty()

取栈顶值(不出栈)

stack.peek()

进栈

stack.push(Object);

出栈

stack.pop();

 示例

public class Test01 {
    public static void main(String[] args) {
        Stack stack=new Stack();
        //1.empty()栈是否为空
        System.out.println(stack.empty());
        //2.peek()栈顶值    3.进栈push()
        stack.push(new Integer(1));
        stack.push("b");
        System.out.println(stack.peek());
        //4.pop()出栈
        stack.pop();
        System.out.println(stack.peek());
        
    }
}

输出结果

true
b
1

posted @ 2017-10-24 17:59  不爱熬夜的猫  阅读(32324)  评论(0编辑  收藏  举报