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;
    }
Get the capacity of the current Vector

size()

public synchronized int size() {
        return elementCount;
    }
Get the number of current Vector elements

isEmpty()

public synchronized boolean isEmpty() {
        return elementCount == 0;
    }
Determine if it is empty

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");
            }
        };
    }
Get an Enumeration type component, you can access Vector elements sequentially, which is very similar to an iterator

contains(Object o)

public boolean contains(Object o) {
        return indexOf(o, 0) >= 0;
    }
Check if element o is included

indexOf(Object o)

public int indexOf(Object o) {
        return indexOf(o, 0);
    }
Returns the index of the first occurrence of element o

indexOf(Object o, intindex)

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

Starting from the index, find the subscript where the element o appears, and  
return -1 when it does not appear. The  
implementation is very simple to search sequentially, and compare whether they are equal

If there is any error, please leave a message or weixin: qiushuzhao222

Guess you like

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