java.util.Stack類簡介

Stack是一個後進先出(last in first out,LIFO)的堆棧,在Vector類的基礎上擴展5個方法而來

Deque(雙端隊列)比起Stack具有更好的完整性和一致性,應該被優先使用

[plain] view plain copy
  1. E push(E item)   
  2.          把項壓入堆棧頂部。   
  3. E pop()   
  4.          移除堆棧頂部的對象,並作爲此函數的值返回該對象。   
  5. E peek()   
  6.          查看堆棧頂部的對象,但不從堆棧中移除它。   
  7. boolean empty()   
  8.          測試堆棧是否爲空。    
  9. int search(Object o)   
  10.          返回對象在堆棧中的位置,以 1 爲基數。  

Stack本身通過擴展Vector而來,而Vector本身是一個可增長的對象數組( a growable array of objects)那麼這個數組的哪裏作爲Stack的棧頂,哪裏作爲Stack的棧底?

答案只能從源代碼中尋找,jdk1.6:

[java] view plain copy
  1. public class Stack<E> extends Vector<E> {  
  2.     /** 
  3.      * Creates an empty Stack. 
  4.      */  
  5.     public Stack() {  
  6.     }  
  7.   
  8.     /** 
  9.      * Pushes an item onto the top of this stack. This has exactly 
  10.      * the same effect as: 
  11.      * <blockquote><pre> 
  12.      * addElement(item)</pre></blockquote> 
  13.      * 
  14.      * @param   item   the item to be pushed onto this stack. 
  15.      * @return  the <code>item</code> argument. 
  16.      * @see     java.util.Vector#addElement 
  17.      */  
  18.     public E push(E item) {  
  19.     addElement(item);  
  20.   
  21.     return item;  
  22.     }  
  23.   
  24.     /** 
  25.      * Removes the object at the top of this stack and returns that 
  26.      * object as the value of this function. 
  27.      * 
  28.      * @return     The object at the top of this stack (the last item 
  29.      *             of the <tt>Vector</tt> object). 
  30.      * @exception  EmptyStackException  if this stack is empty. 
  31.      */  
  32.     public synchronized E pop() {  
  33.     E   obj;  
  34.     int len = size();  
  35.   
  36.     obj = peek();  
  37.     removeElementAt(len - 1);  
  38.   
  39.     return obj;  
  40.     }  
  41.   
  42.     /** 
  43.      * Looks at the object at the top of this stack without removing it 
  44.      * from the stack. 
  45.      * 
  46.      * @return     the object at the top of this stack (the last item 
  47.      *             of the <tt>Vector</tt> object). 
  48.      * @exception  EmptyStackException  if this stack is empty. 
  49.      */  
  50.     public synchronized E peek() {  
  51.     int len = size();  
  52.   
  53.     if (len == 0)  
  54.         throw new EmptyStackException();  
  55.     return elementAt(len - 1);  
  56.     }  
  57.   
  58.     /** 
  59.      * Tests if this stack is empty. 
  60.      * 
  61.      * @return  <code>true</code> if and only if this stack contains 
  62.      *          no items; <code>false</code> otherwise. 
  63.      */  
  64.     public boolean empty() {  
  65.     return size() == 0;  
  66.     }  
  67.   
  68.     /** 
  69.      * Returns the 1-based position where an object is on this stack. 
  70.      * If the object <tt>o</tt> occurs as an item in this stack, this 
  71.      * method returns the distance from the top of the stack of the 
  72.      * occurrence nearest the top of the stack; the topmost item on the 
  73.      * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt> 
  74.      * method is used to compare <tt>o</tt> to the 
  75.      * items in this stack. 
  76.      * 
  77.      * @param   o   the desired object. 
  78.      * @return  the 1-based position from the top of the stack where 
  79.      *          the object is located; the return value <code>-1</code> 
  80.      *          indicates that the object is not on the stack. 
  81.      */  
  82.     public synchronized int search(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類爲線程安全類(多個方法調用而產生的數據不一致問題屬於原子性問題的範疇)

[java] view plain copy
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         Stack<String> s = new Stack<String>();  
  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.         Test.it(s);  
  11.         System.out.println("------pop");  
  12.         String str = s.pop();  
  13.         System.out.println(str);  
  14.         Test.it(s);  
  15.         System.out.println("------peek");  
  16.         str = s.peek();  
  17.         System.out.println(str);  
  18.         Test.it(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 it(Stack<String> s){  
  29.         System.out.print("iterator:");  
  30.         Iterator<String> it = s.iterator();  
  31.         while(it.hasNext()){  
  32.             System.out.print(it.next()+";");  
  33.         }  
  34.         System.out.print("\n");  
  35.     }  
  36. }  

結果:

[sql] view plain copy
  1. ------isEmpty  
  2. true            
  3. ------push  
  4. iterator:1;2;3;    
  5. ------pop  
  6. 3       --棧頂是數組最後一個  
  7. iterator:1;2;  
  8. ------peek  
  9. 2       --pop取後刪掉,peek只取不刪  
  10. iterator:1;2;  
  11. ------search      
  12. 1       --以1爲基數,即棧頂爲1  
  13. 2       --和棧頂見的距離爲2-1=1  
  14. -1      --不存在於棧中  

Stack並不要求其中保存數據的唯一性,當Stack中有多個相同的item時,調用search方法,只返回與查找對象equal並且離棧頂最近的item與棧頂間距離(見源碼中search方法說明)

發佈了130 篇原創文章 · 獲贊 138 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章