`
Javaloverlover
  • 浏览: 343470 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

java集合类——Stack类

    博客分类:
  • java
阅读更多
查看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 empty
5
5
4
2


可以看出
分享到:
评论
1 楼 qj200040 2011-08-22  
定义一个数组,然后定好长度,top=-1;
push(int num) nums[++top]=num;
int pop () return nump[top--];
好像就这么多吧。。

相关推荐

Global site tag (gtag.js) - Google Analytics