码迷,mamicode.com
首页 > 编程语言 > 详细

java集合类——Stack栈类

时间:2015-02-04 18:25:40      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

今日走读代码时,遇到stack栈类,特查看java的API文档,总结如下:

Stack继承Vector类。
栈的特点是后进先出。
API中Stack自身的方法不多,基本跟栈的特点有关。

现附上例子,后续继续总结

/**
 * @作者 whs
 * @创建日期 2015年2月4日
 * @版本 V 1.0
 */
package thread.pool;

import java.util.Stack;

public class StackExam {
	
	public static void main(String[] args) {
		Stack<String> stack = new Stack<String>();
		System.out.println("now the satck is "+isEmpty(stack));
		stack.push("1");
		stack.push("2");
		stack.push("3");
		stack.push("4");
		stack.push("5");
		stack.push("6");
		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("3"));//,此方法返回最近的目标对象距堆栈顶部出现位置到堆栈顶部的距离;
	}
	
	public static String isEmpty(Stack<String> stack){
		return stack.empty() ? "empty":"not empty";
	}
	
}

 输出为:

now the satck is empty
now the stack is not empty
6
6
5
2

 

java集合类——Stack栈类

标签:

原文地址:http://www.cnblogs.com/whsa/p/4272717.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!