Stack是一个后进先出(last in first out,LIFO)的堆栈,在Vector类的基础上扩展5个方法而来
Deque(双端队列)比起stack具有更好的完整性和一致性,应该被优先使用
1 E push(E item)2 把项压入堆栈顶部。3 E pop()4 移除堆栈顶部的对象,并作为此函数的值返回该对象。5 E peek()6 查看堆栈顶部的对象,但不从堆栈中移除它。7 booleanempty()8 测试堆栈是否为空。9 intsearch(Object o)10 返回对象在堆栈中的位置,以 1 为基数。
Stack本身通过扩展Vector而来,而Vector本身是一个可增长的对象数组( a growable array of objects)那么这个数组的哪里作为Stack的栈顶,哪里作为Stack的栈底?
答案只能从源代码中寻找,jdk1.6
1 public class Stack extends Vector{2 /**
3 * Creates an empty Stack.4 */
5 publicStack() {6 }7
8 /**
9 * Pushes an item onto the top of this stack. This has exactly10 * the same effect as:11 *
13 *14 *@paramitem the item to be pushed onto this stack.15 *@returnthe12 * addElement(item)
item
argument.16 *@seejava.util.Vector#addElement17 */
18 publicE push(E item) {19 addElement(item);20
21 returnitem;22 }23
24 /**
25 * Removes the object at the top of this stack and returns that26 * object as the value of this function.27 *28 *@returnThe object at the top of this stack (the last item29 * of the Vector object).30 *@exceptionEmptyStackException if this stack is empty.31 */
32 public synchronizedE pop() {33 E obj;34 int len =size();35
36 obj =peek();37 removeElementAt(len - 1);38
39 returnobj;40 }41
42 /**
43 * Looks at the object at the top of this stack without removing it44 * from the stack.45 *46 *@returnthe object at the top of this stack (the last item47 * of the Vector object).48 *@exceptionEmptyStackException if this stack is empty.49 */
50 public synchronizedE peek() {51 int len =size();52
53 if (len == 0)54 throw newEmptyStackException();55 return elementAt(len - 1);56 }57
58 /**
59 * Tests if this stack is empty.60 *61 *@returntrue
if and only if this stack contains62 * no items; false
otherwise.63 */
64 public booleanempty() {65 return size() == 0;66 }67
68 /**
69 * Returns the 1-based position where an object is on this stack.70 * If the object o occurs as an item in this stack, this71 * method returns the distance from the top of the stack of the72 * occurrence nearest the top of the stack; the topmost item on the73 * stack is considered to be at distance 1. The equals74 * method is used to compare o to the75 * items in this stack.76 *77 *@paramo the desired object.78 *@returnthe 1-based position from the top of the stack where79 * the object is located; the return value -1
80 * indicates that the object is not on the stack.81 */
82 public synchronized intsearch(Object o) {83 int i =lastIndexOf(o);84
85 if (i >= 0) {86 return size() -i;87 }88 return -1;89 }90
91 /**use serialVersionUID from JDK 1.0.2 for interoperability*/
92 private static final long serialVersionUID = 1224463164541339165L;93 }
通过peek()方法注释The object at the top of this stack (the last item of the Vector object,可以发现
数组(Vector)的最后一位即为Stack的栈顶
pop、peek以及search方法本身进行了同步
push方法调用了父类的addElement方法
empty方法调用了父类的size方法
Vector类为线程安全类
综上,Stack类为线程安全类(多个方法调用而产生的数据不一致问题属于原子性问题的范畴)
1 public classStackTest {2 public static voidmain(String[] args) {3 Stack s = new Stack();4 System.out.println("------isEmpty");5 System.out.println(s.isEmpty());6 System.out.println("------push");7 s.push("1");8 s.push("2");9 s.push("3");10 StackTest.t(s);11 System.out.println("------pop");12 String str =s.pop();13 System.out.println(str);14 StackTest.t(s);15 System.out.println("------peek");16 str =s.peek();17 System.out.println(str);18 StackTest.t(s);19 System.out.println("------search");20 int i = s.search("2");21 System.out.println(i);22 i = s.search("1");23 System.out.println(i);24 i = s.search("none");25 System.out.println(i);26 }27
28 public static void t(Stacks){29 System.out.println("iterator");30 Iterator it =s.iterator();31 while(it.hasNext()){32 System.out.print(it.next()+" ");33 }34 System.out.println("\n");35 }36 }
结果:
1 ------isEmpty2 true
3 ------push4 iterator:1;2;3;5 ------pop6 3 --栈顶是数组最后一个7 iterator:1;2;8 ------peek9 2 --pop取后删掉,peek只取不删10 iterator:1;2;11 ------search12 1 --以1为基数,即栈顶为113 2 --和栈顶见的距离为2-1=1
14 -1 --不存在于栈中
Stack并不要求其中保存数据的唯一性,当Stack中有多个相同的item时,调用search方法,只返回与查找对象equal并且离栈顶最近的item与栈顶间距离(见源码中search方法说明)