Java 集合系列07之 Stack詳細介紹(源碼解析)和使用示例

 

概要

學完Vector了以後,接下來咱們開始學習Stack。Stack很簡單,它繼承於Vector。學習方式仍是和以前同樣,先對Stack有個總體認識,而後再學習它的源碼;最後再經過實例來學會使用它。內容包括:
第1部分 Stack介紹
第2部分 Stack源碼解析(基於JDK1.6.0_45)
第3部分 Vector示例
html

轉載請註明出處:http://www.cnblogs.com/skywang12345/p/3308852.htmljava

 

第1部分 Stack介紹

Stack簡介數組

Stack是棧。它的特性是:先進後出(FILO, First In Last Out)。數據結構

java工具包中的Stack是繼承於Vector(矢量隊列)的,因爲Vector是經過數組實現的,這就意味着,Stack也是經過數組實現的而非鏈表。固然,咱們也能夠將LinkedList看成棧來使用!在「Java 集合系列06之 Vector詳細介紹(源碼解析)和使用示例」中,已經詳細介紹過Vector的數據結構,這裏就再也不對Stack的數據結構進行說明了。dom

 

Stack的繼承關係ide

java.lang.Object
↳     java.util.AbstractCollection<E>
   ↳     java.util.AbstractList<E>
       ↳     java.util.Vector<E>
           ↳     java.util.Stack<E>

public class Stack<E> extends Vector<E> {}

 

Stack和Collection的關係以下圖函數

 

Stack的構造函數工具

Stack只有一個默認構造函數,以下:post

Stack()

 

Stack的API學習

Stack是棧,它經常使用的API以下:

             boolean       empty()
synchronized E             peek()
synchronized E             pop()
             E             push(E object)
synchronized int           search(Object o)

 

因爲Stack和繼承於Vector,所以它也包含Vector中的所有API

 

第2部分 Stack源碼解析(基於JDK1.6.0_45)

Stack的源碼很是簡單,下面咱們對它進行學習。 

 1 package java.util;
 2 
 3 public
 4 class Stack<E> extends Vector<E> {
 5     // 版本ID。這個用於版本升級控制,這裏不須理會!
 6     private static final long serialVersionUID = 1224463164541339165L;
 7 
 8     // 構造函數
 9     public Stack() {
10     }
11 
12     // push函數:將元素存入棧頂
13     public E push(E item) {
14         // 將元素存入棧頂。
15         // addElement()的實如今Vector.java中
16         addElement(item);
17 
18         return item;
19     }
20 
21     // pop函數:返回棧頂元素,並將其從棧中刪除
22     public synchronized E pop() {
23         E    obj;
24         int    len = size();
25 
26         obj = peek();
27         // 刪除棧頂元素,removeElementAt()的實如今Vector.java中
28         removeElementAt(len - 1);
29 
30         return obj;
31     }
32 
33     // peek函數:返回棧頂元素,不執行刪除操做
34     public synchronized E peek() {
35         int    len = size();
36 
37         if (len == 0)
38             throw new EmptyStackException();
39         // 返回棧頂元素,elementAt()具體實如今Vector.java中
40         return elementAt(len - 1);
41     }
42 
43     // 棧是否爲空
44     public boolean empty() {
45         return size() == 0;
46     }
47 
48     // 查找「元素o」在棧中的位置:由棧底向棧頂方向數
49     public synchronized int search(Object o) {
50         // 獲取元素索引,elementAt()具體實如今Vector.java中
51         int i = lastIndexOf(o);
52 
53         if (i >= 0) {
54             return size() - i;
55         }
56         return -1;
57     }
58 }
View Code

總結

(01) Stack實際上也是經過數組去實現的。
       執行push時(即,將元素推入棧中),是經過將元素追加的數組的末尾中。
       執行peek時(即,取出棧頂元素,不執行刪除),是返回數組末尾的元素。
       執行pop時(即,取出棧頂元素,並將該元素從棧中刪除),是取出數組末尾的元素,而後將該元素從數組中刪除。
(02) Stack繼承於Vector,意味着Vector擁有的屬性和功能,Stack都擁有。

  

第3部分 Vector示例

下面咱們經過實例學習如何使用Stack

 1 import java.util.Stack;
 2 import java.util.Iterator;
 3 import java.util.List;
 4 
 5 /**
 6  * @desc Stack的測試程序。測試經常使用API的用法
 7  *
 8  * @author skywang
 9  */
10 public class StackTest {
11 
12     public static void main(String[] args) {
13         Stack stack = new Stack();
14         // 將1,2,3,4,5添加到棧中
15         for(int i=1; i<6; i++) {
16             stack.push(String.valueOf(i));
17         }
18 
19         // 遍歷並打印出該棧
20         iteratorThroughRandomAccess(stack) ;
21 
22         // 查找「2」在棧中的位置,並輸出
23         int pos = stack.search("2");
24         System.out.println("the postion of 2 is:"+pos);
25 
26         // pup棧頂元素以後,遍歷棧
27         stack.pop();
28         iteratorThroughRandomAccess(stack) ;
29 
30         // peek棧頂元素以後,遍歷棧
31         String val = (String)stack.peek();
32         System.out.println("peek:"+val);
33         iteratorThroughRandomAccess(stack) ;
34 
35         // 經過Iterator去遍歷Stack
36         iteratorThroughIterator(stack) ;
37     }
38 
39     /**
40      * 經過快速訪問遍歷Stack
41      */
42     public static void iteratorThroughRandomAccess(List list) {
43         String val = null;
44         for (int i=0; i<list.size(); i++) {
45             val = (String)list.get(i);
46             System.out.print(val+" ");
47         }
48         System.out.println();
49     }
50 
51     /**
52      * 經過迭代器遍歷Stack
53      */
54     public static void iteratorThroughIterator(List list) {
55 
56         String val = null;
57         for(Iterator iter = list.iterator(); iter.hasNext(); ) {
58             val = (String)iter.next();
59             System.out.print(val+" ");
60         }
61         System.out.println();
62     }
63 
64 }
View Code

運行結果: 

1 2 3 4 5 
the postion of 2 is:4
1 2 3 4 
peek:4
1 2 3 4 
1 2 3 4 
相關文章
相關標籤/搜索