JDK source code-Vector, jdk-vector
1, Vector:-1, an array of objects that can grow. Similar to an object array, it contains an integer index for component access. However, the Vector size can be increased or reduced as needed.
-2. Each Vector optimizes storage management by maintaining capacity and capacityIncrement. Capacity is always equal to at least the Vector size. Generally, this value is greater than the capacity of the Vector because the Vector increases the storage block by capacityIncrement. Before adding a large amount of data, the application can manually increase the Vector capacity to reduce the amount of allocated data. 2. Inheritance structure:
3. member variables:-1, elementData: array objects are used to store elements in a Vector. The Capacity length is the length of the array.
protected Object[] elementData;
-2, elementCount: number of elements in the array.
protected int elementCount;
-3, capacityIncrement: array growth base.
protected int capacityIncrement;
4. constructor-1, Vector (int, int)
public Vector(int initialCapacity, int capacityIncrement) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
This. elementData = new Object [initialCapacity]; // initializes the Array
This. capacityIncrement = capacityIncrement; // initialize the default Growth Value
}
-2, Vector (): Empty constructor. The default initialization array length is 10, and the default growth parameter is 0.
public Vector() {
this(10);
}
-3, Vector (int ):
public Vector(int initialCapacity) {
this(initialCapacity, 0);
}
5. The main method and the method operations in the Vector are synchronous operations. -1, addElement
public synchronized void addElement(E obj) {
ModCount ++; // Add 1 to the structured modification Parameter
EnsureCapacityHelper (elementCount + 1); // ensure that the capacity size meets the requirements for adding elements
ElementData [elementCount ++] = obj; // storage element, array element index plus 1
}
private void ensureCapacityHelper(int minCapacity) {
Int oldCapacity = elementData. length; // obtain the array length.
If (minCapacity> oldCapacity) {// if the index of the currently stored element is greater than the array Length
Object[] oldData = elementData;
int newCapacity = (capacityIncrement > 0) ?
(OldCapacity + capacityIncrement): (oldCapacity * 2); // The default normal length of each array length increase. When the default length increases by 0, the extended array length is twice the original length.
if (newCapacity < minCapacity) {
newCapacity = minCapacity;
}
ElementData = Arrays. copyOf (elementData, newCapacity); // copy an array
}
}
-2, removeObject (Object object): removes an element. If multiple elements exist, the first element is removed.
public synchronized boolean removeElement(Object obj) {
ModCount ++; // Add 1 to the structured modification count
Int I = indexOf (obj); // obtain the element location
if (i >= 0) {
RemoveElementAt (I); // remove the element from the corresponding position
return true;
}
return false;
}
public int indexOf(Object o) {
return indexOf(o, 0);
}
public synchronized int indexOf(Object o, int index) {
If (o = null) {// if the element is null, use = to compare equal elements.
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]) // if the element is not null, call the equals method to determine whether the element is equal.
return i;
}
return -1;
}
-3, add (E)
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
-4, capacity: obtains the length of an internal array.
public synchronized int capacity() {
return elementData.length;
}
-5, size: Get the length of the corresponding Array Storage Element
public synchronized int size() {
return elementCount;
}