JDK source code-Vector, jdk-vector

Source: Internet
Author: User

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 ArrayThis. 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 ParameterEnsureCapacityHelper (elementCount + 1); // ensure that the capacity size meets the requirements for adding elementsElementData [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 countInt I = indexOf (obj); // obtain the element locationif (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;    }




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.