This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

This article was first published on CSDN, so there are also CSDN watermarks on the pictures. All the articles are original and there is no infringement

Vector is a dynamic array and an underlying array. A Vector is very similar to an ArrayList except that it is different in some details. For example, Vector is thread-safe and has twice the default capacity

1. Inheritance system

  • List interface, with the collection of the basic framework, such as collection add, delete, modify
  • Implement RandomAccess interface, with the characteristics of RandomAccess, in fact, Vector is an array at the bottom, with the characteristics of RandomAccess
  • Cloneable interface, can be cloned, but here is a shallow clone clone
  • The Serializable interface is implemented and can be serialized and deserialized

2. Member variables

/* The member variables of Vector are protected, and the member variables of ArrayList are private. Private variables can only be called in the same class. Protected variables can only be called in the same package or in a different package subclass. Protected int elementCount; Protected int capacityIncrement; // If the value is 0, the value is twice as large as the original value; if the value is greater than 0, the value is greater than the value. private static final long serialVersionUID = -2767605614048989439L;Copy the code

3. Construction method

Public Vector() {this(10); Public Vector(int initialCapacity) {this(initialCapacity, 0);} public Vector(int initialCapacity) {this(initialCapacity, 0); } 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(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); }Copy the code

4. Membership method

get(int index)

Gets the data at the specified subscript

Public synchronized E get(int index) {if (index >= elementCount) An exception is thrown throw new ArrayIndexOutOfBoundsException (index); return elementData(index); }Copy the code
set(int index, E element)

Modifies the data element at the specified subscript

public synchronized E set(int index, E element) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);

        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }
Copy the code
remove(Object o)

Deletes the specified data element

public boolean remove(Object o) { return removeElement(o); } public synchronized boolean removeElement(Object obj) { modCount++; Int I = indexOf(obj); If (I >= 0) {removeElementAt(I); // Remove elements by subscript return true; } return false; } public synchronized void removeElementAt(int index) { modCount++; / / set the underlying modify number if (index > = elementCount) {/ / subscript cross-border throw new ArrayIndexOutOfBoundsException (index + "> =" + elementCount); } else if (index < 0) {/ / illegal throw new ArrayIndexOutOfBoundsException index (index); } int j = elementcount-index-1; if (j > 0) { System.arraycopy(elementData, index + 1, elementData, index, j); } elementCount--; ElementData [elementCount] = null; Public synchronized E remove(int index) {modCount++; public synchronized E remove(int index) {modCount++; / / set the underlying modify number if (index > = elementCount) / / subscript illegal throw new ArrayIndexOutOfBoundsException (index); E oldValue = elementData(index); int numMoved = elementCount - index - 1; If (numMoved > 0) system. arrayCopy (elementData, index+1, elementData, index, numMoved); //copy elementData[--elementCount] = null; // Let gc do its work return oldValue; // return the element at the index before dropping the specified index}Copy the code
add(E e)

Adds an element to the end of the collection

public synchronized boolean add(E e) { modCount++; EnsureCapacityHelper (elementCount + 1); ElementData [elementCount++] = e; return true; }Copy the code

5. Other methods

// Copy method, Public synchronized void copyInto(Object[] anArray) {system. arrayCopy (elementData, 0, synchronized void copyInto(Object[] anArray) {system. arrayCopy (elementData, 0, anArray, 0, elementCount); Synchronized void trimToSize() {modCount++; synchronized void trimToSize(); synchronized void trimToSize(); Int oldCapacity = elementData.length; If (elementCount < oldCapacity) {// Specify that the actual number of stored elements is less than the capacity of the collection // place the first elementCount elements in an array and return elementData = Arrays.copyOf(elementData, elementCount); Synchronized void setSize(int newSize) {modCount++; synchronized void setSize(int newSize) {synchronized void setSize(int newSize); If (newSize > elementCount) {// The new capacity is greater than the actual number of elements stored in the array ensureCapacityHelper(newSize); } else {for (int I = newSize; i < elementCount ; ElementData [I] = null; elementData[I] = null; } } elementCount = newSize; Public synchronized int capacity() {return elementdata.length; synchronized int capacity() {return elementdata.length; } public synchronized int size() {return elementCount; } public Boolean contains(Object o) {return indexOf(o, 0) >= 0; Public int indexOf(Object o) {return indexOf(o, 0); } public synchronized int indexOf(Object o, synchronized int indexOf(Object o, synchronized int indexOf); 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; } // find the subscript return-1 for the position of the element; // Return -1}Copy the code

6. Capacity expansion mechanism

Public synchronized void ensureCapacity(int minCapacity) {if (minCapacity > 0) {modCount++; ensureCapacityHelper(minCapacity); Private void ensureCapacityHelper(int minCapacity) {// overflow-conscious code if (minCapacity -) Elementdata.length > 0) // Grow (minCapacity); } private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; // The capacity of the old array // if the growth coefficient is 0, the capacity is doubled; Int newCapacity = oldCapacity + ((capacityIncrement > 0)? capacityIncrement : oldCapacity); If (newcapacity-mincapacity < 0) // The capacity after expansion is insufficient, set the capacity to the minimum required capacity newCapacity = minCapacity. If (newCapacity -max_array_size > 0) // The expanded capacity is greater than the maximum array length newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); Private static int hugeCapacity(int minCapacity) {if (minCapacity < 0) // overflow // minCapacity < 0, Throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? // If the minimum size is greater than the maximum length of the array, set the capacity to the maximum value of the Integer, otherwise set the capacity to the maximum array length integer. MAX_VALUE: MAX_ARRAY_SIZE; }Copy the code

7. To summarize

  • The underlying Vector is an array. When we construct a Vector with no arguments, the default size is 10

  • The expansion of a Vector is related to the growth factor. If the growth factor is 0, the array is doubled each time it is expanded, otherwise, the growth factor is the size of the digit increase in the array

  • Synchronized is added to many of Vector’s methods to indicate thread-safety

  • Many of Vector’s methods are the same as Those of ArrayList, so here are just a few of them. See the source code for ArrayList for details