Stack

来源:互联网 发布:亚马逊中国 知乎 编辑:程序博客网 时间:2024/05/12 05:46

java中Stack常用方法,
1.empty()判断栈中是否为空。
2.peek()从栈顶取值,不删除该值
3.pop()从栈顶取值,删除改值
4.push(E item) 往stack中添加值
5.search(Object o) 查询stack中某一个值所在的位置指标,若是没有则返回-1

import java.util.Stack;public class StackTest {    public static void main(String args[]) {    Stack<String> stack = new Stack<String>();    System.out.println("now the stack is:" + isEmpty(stack));    stack.push("a");    stack.push("b");    stack.push("c");    stack.push("d");    stack.push("e");    System.out.println("now the stack is:" + isEmpty(stack));    System.out.println(stack.peek());    System.out.println(stack.pop());    System.out.println(stack.pop());    System.out.println(stack.search("c"));    }    public static String isEmpty(Stack<String> stack) {    return stack.empty() ? "empty" : "not empty";    }}

运行结果:
now the stack is:empty
now the stack is:not empty
e
e
d
1

0 0
原创粉丝点击