您的位置:首页 > 编程语言 > Java开发

java集合类——Stack类

2016-03-04 17:10 375 查看
查看java的API文档,Stack继承Vector类。
栈的特点是后进先出。

API中Stack自身的方法不多,基本跟栈的特点有关。

Java代码


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("1");

stack.push("2");

stack.push("3");

stack.push("4");

stack.push("5");

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("2"));

}

public static String isEmpty(Stack<String> stack) {

return stack.empty() ? "empty" : "not empty";

}

}

输出为:

Java代码


now the stack is empty

now the stack is not empty

5

5

4

2

可以看出
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: