六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 8|回复: 0

java集合类——Stack类

[复制链接]

升级  34.33%

93

主题

93

主题

93

主题

举人

Rank: 3Rank: 3

积分
303
 楼主| 发表于 2013-2-3 11:17:31 | 显示全部楼层 |阅读模式
查看java的API文档,Stack继承Vector类。
栈的特点是后进先出。
API中Stack自身的方法不多,基本跟栈的特点有关。

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";}}

输出为:
now the stack is not empty5542

可以看出
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表