今日走读代码时,遇到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
原文:http://www.cnblogs.com/whsa/p/4272717.html