1. 程式人生 > >Vector 源碼分析 jdk1.8

Vector 源碼分析 jdk1.8

ensure 訪問 class 遍歷 pre 元素 www tro col

Vector簡介

Vector 是矢量隊列,它是JDK1.0版本添加的類。繼承於AbstractList,實現了List, RandomAccess, Cloneable這些接口。
Vector 繼承了AbstractList,實現了List;所以,它是一個隊列,支持相關的添加、刪除、修改、遍歷等功能
Vector 實現了RandmoAccess接口,即提供了隨機訪問功能。RandmoAccess是java中用來被List實現,為List提供快速訪問功能的。在Vector中,我們即可以通過元素的序號快速獲取元素對象;這就是快速隨機訪問。
Vector 實現了Cloneable接口,即實現clone()函數。它能被克隆。

和ArrayList不同,Vector中的操作是線程安全的

Vector的源碼分析

  Vector 源碼和 ArrAyList 基本一致, 。如需 ArrAyList 源碼分析,詳情請見 http://www.cnblogs.com/scholar-xie/p/7000082.html

  差異區別

  

  /**
     * synchronized*/
    public synchronized void addElement(E obj) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount
++] = obj; } /** * synchronized */ public synchronized void removeAllElements() { modCount++; // Let gc do its work for (int i = 0; i < elementCount; i++) elementData[i] = null; elementCount = 0; } /** * 方法上面加上了 synchronized*/ public
synchronized E get(int index) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); return elementData(index); }

  

Vector 源碼分析 jdk1.8