Vector source code interpretation

Introduction to Vector

 1. Internally implemented through an array  2. Through the synchronized synchronization method, thread-safe, suitable for multi-threading 3. Due to thread safety, the efficiency is not high  4. The default is to store 10 elements  

   

 5. When the capacity needs to be increased, the default new capacity is the size of the element Vector. 6. The efficiency is low and it is not recommended.  

Attributes

// Internally store elements through the Object array
protected Object[] elementData;

// current element id
protected int elementCount;

// can increase the capacity size each time
protected int capacityIncrement;

Constructor 

    public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
        this.capacityIncrement = capacityIncrement;
    }

initalCapacity initializes the capacity size, the size of each expansion of capacityIncrement

public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }
Initially specified capacity size
public Vector() {
        this(10);
    }
Only 10 elements can be stored by default
/* @throws NullPointerException if the specified collection is null
     * @since   1.2
     */
    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);
    }
Constructing Vector from set c

core method

copyInto(Object[] anArray):


public synchronized void copyInto(Object[] anArray) {
        System.arraycopy(elementData, 0, anArray, 0, elementCount);
    }
The following methods are all synchronized to ensure thread safety  
. The elements in elementData are copied to anArray

trimToSize():

public synchronized void trimToSize() {
        modCount++;
        int oldCapacity = elementData.length;
        if (elementCount < oldCapacity) {
            elementData = Arrays.copyOf(elementData, elementCount);
        }
    }
Trim elementData to ensure that the internal elements are all valid elements  
elementCount records the actual number of elements

ensureCapacity(int minCapacity)

public synchronized void ensureCapacity(int minCapacity) {
        if (minCapacity > 0) {
            modCount++;
            ensureCapacityHelper(minCapacity);//Whether the capacity needs to be increased
        }
    }
increase capacity

ensureCapacityHelper(int minCapacity)

private void ensureCapacityHelper(int minCapacity) {
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)// true increase capacity
            grow(minCapacity);
    }
According to the length of Vector and judging whether the capacity needs to be increased

grow(int minCapacity)

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);
    }

    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

capacity()

public synchronized int capacity() {
        return elementData.length;
    }
获取当前Vector的容量

size()

public synchronized int size() {
        return elementCount;
    }
获取当前Vector元素的个数

isEmpty()

public synchronized boolean isEmpty() {
        return elementCount == 0;
    }
判断是否为空

elements()

public Enumeration<E> elements() {
        return new Enumeration<E>() {
            int count = 0;

            public boolean hasMoreElements() {
                return count < elementCount;
            }

            public E nextElement() {
                synchronized (Vector.this) {
                    if (count < elementCount) {
                        return elementData(count++);
                    }
                }
                throw new NoSuchElementException("Vector Enumeration");
            }
        };
    }
获取一个Enumeration类型的组件,可以顺序的访问Vector元素,这个和迭代器很类似

contains(Object o)

public boolean contains(Object o) {
        return indexOf(o, 0) >= 0;
    }
判断是否包含元素 o

indexOf(Object o)

public int indexOf(Object o) {
        return indexOf(o, 0);
    }
返回元素 o 第一次出现的下标

indexOf(Object o, int index)

 public synchronized int indexOf(Object o, int index) {
        if (o == null) {
            for (int i = index ; i < elementCount ; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = index ; i < elementCount ; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

从index开始,查找元素o出现的下标 
当不出现的时候返回-1 
实现很简单顺序查找,比较是否相等

如有错误请留言或weixin:qiushuzhao222

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324728325&siteId=291194637