国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Vector與Stack源碼分析

2019-11-11 04:15:32
字體:
來源:轉載
供稿:網友

Vector類從JDk 1.0開始就存在了,其類的繼承關系如下: Vector繼承關系圖 從上圖可以發現Vector的繼承關系與ArrayList的繼承關系相同,功能上Vector與ArrayList也是一樣的,都是一個擴展的列表。區別在于Vector是線程安全的,而ArrayList不是線程安全的。 Stack類代表的是一個LIFO的棧,該類的繼承關系如下: Stack繼承關系圖 可以看到Stack繼承自Vector類,在Vector的基礎上提供了5個操作使Vector具備Stack的性質。五個方法分別是push、pop、peek、檢測棧是否為空基于一個搜索方法。 下面先介紹Vector的源碼,最后介紹一下Stack。

Vector源碼介紹

構造器

Vector有四個構造方法,其內部有兩個重要的參數,一個是elementCount代表當前元素個數,一個是capacityIncrement,代表當列表元素滿了之后增加的容量。如果不設置capacityIncrement,那么Vector容量擴展時默認將擴展兩倍,在ArrayList源碼分析中,我們知道ArrayList在擴容時默認將擴展1.5倍,所以這又是ArrayList與Vector的一個區別。

public Vector(int initialCapacity, int capacityIncrement) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; this.capacityIncrement = capacityIncrement; }public Vector(int initialCapacity) { this(initialCapacity, 0); } public Vector() { this(10); } public Vector(Collection<? extends E> c) { elementData = c.toArray(); elementCount = elementData.length; // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, elementCount, Object[].class); }

從上面代碼還可以看到,Vector初始時容量為10,而ArrayList初始容量為0。

添加操作

下面就以add(E e)方法為例介紹一個,其余方法與ArrayList中的流程類似。add()方法的實現如下:

public synchronized boolean add(E e) { modCount++; //確保容量足夠 ensureCapacityHelper(elementCount + 1); //添加元素 elementData[elementCount++] = e; return true; }

從上面代碼可以看到,add()方法用synchronized關鍵字修飾,所以是線程安全的,ensureCapacityHelper()方法用于確保容量足夠,不夠時擴展容量,其實現如下:

PRivate void ensureCapacityHelper(int minCapacity) { // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); }

可以看到,當需要擴容時,將調用grow()方法。

private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); }

可以看到,如果capacityIncrement為0時,那么newCapacity將會是兩倍的oldCapacity,后面的操作與ArrayList中的相同。下面看一下ArrayList的grow方法可以看到區別。

private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); }

從上面就可以看出兩個類在得到newCapacity變量時的區別。 其他add方法與ArrayList相同,只不過每個方法多了synchronized關鍵字修飾。

其余操作

其余操作與ArrayList類似,只不過每個方法都多了synchronized關鍵字,從而保證了Vector類的線程安全。

Stack源碼分析

Stack類的實現比較簡單,只是在Vector的基礎上添加了一個方法,下面是Stack類的實現:

publicclass Stack<E> extends Vector<E> { /** * Creates an empty Stack. */ public Stack() { } /** * Pushes an item onto the top of this stack. This has exactly * the same effect as: * <blockquote><pre> * addElement(item)</pre></blockquote> * * @param item the item to be pushed onto this stack. * @return the <code>item</code> argument. * @see java.util.Vector#addElement */ public E push(E item) { addElement(item); return item; } /** * Removes the object at the top of this stack and returns that * object as the value of this function. * * @return The object at the top of this stack (the last item * of the <tt>Vector</tt> object). * @throws EmptyStackException if this stack is empty. */ public synchronized E pop() { E obj; int len = size(); obj = peek(); removeElementAt(len - 1); return obj; } /** * Looks at the object at the top of this stack without removing it * from the stack. * * @return the object at the top of this stack (the last item * of the <tt>Vector</tt> object). * @throws EmptyStackException if this stack is empty. */ public synchronized E peek() { int len = size(); if (len == 0) throw new EmptyStackException(); return elementAt(len - 1); } /** * Tests if this stack is empty. * * @return <code>true</code> if and only if this stack contains * no items; <code>false</code> otherwise. */ public boolean empty() { return size() == 0; } /** * Returns the 1-based position where an object is on this stack. * If the object <tt>o</tt> occurs as an item in this stack, this * method returns the distance from the top of the stack of the * occurrence nearest the top of the stack; the topmost item on the * stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt> * method is used to compare <tt>o</tt> to the * items in this stack. * * @param o the desired object. * @return the 1-based position from the top of the stack where * the object is located; the return value <code>-1</code> * indicates that the object is not on the stack. */ public synchronized int search(Object o) { int i = lastIndexOf(o); if (i >= 0) { return size() - i; } return -1; } /** use serialVersionUID from JDK 1.0.2 for interOperability */ private static final long serialVersionUID = 1224463164541339165L;}

從上面的代碼可以看到,Stack內部一共五個方法,其中三個方法都加了synchronized關鍵字,是線程安全的,而push和empty方法調用了Vector的方法,由于Vector中的addElement()和size()方法都是線程安全的,所以Stack的每個方法也都是線程安全的,所以它和Vector一樣,都是線程安全的。

總結

Vector與ArrayList的最大區別就是Vector是線程安全的,而ArrayList不是線程安全的。另外區別還有: - ArrayList不可以設置擴展的容量,默認1.5倍;Vector可以設置擴展的容量,如果沒有設置,默認2倍 - ArrayList的無參構造方法中初始容量為0,而Vector的無參構造方法中初始容量為10。 - Vector線程安全,ArrayList線程不安全。

Vector和它的子類Stack都是線程安全的集合。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 山西省| 哈巴河县| 华坪县| 石门县| 祁阳县| 安化县| 巢湖市| 福清市| 应城市| 潼关县| 澄迈县| 临沭县| 岢岚县| 宁波市| 昌黎县| 如皋市| 湖北省| 额尔古纳市| 太仓市| 佛坪县| 石门县| 伊川县| 尤溪县| 江城| 通城县| 蒙自县| 襄樊市| 祁门县| 璧山县| 台湾省| 莱阳市| 金寨县| 蚌埠市| 南召县| 资中县| 当雄县| 房产| 隆林| 静宁县| 孙吴县| 农安县|