vector

vector和arrayList一样底层实现原理也是数组 区别: vector是同步的也就是线程安全的[因为vector里面的某些方法用了synchronized 关键字修饰,比如removeAll(),addAll(),equals(),Iterator()等方法被修饰]

部分源码

  

/**
* Inserts all of the elements in the specified Collection into this
* Vector at the specified position. Shifts the element currently at
* that position (if any) and any subsequent elements to the right
* (increases their indices). The new elements will appear in the Vector
* in the order that they are returned by the specified Collection's
* iterator.
*
* @param index index at which to insert the first element from the
* specified collection
* @param c elements to be inserted into this Vector
* @return {@code true} if this Vector changed as a result of the call
* @throws ArrayIndexOutOfBoundsException if the index is out of range
* ({@code index < 0 || index > size()})
* @throws NullPointerException if the specified collection is null
* @since 1.2
*/
public synchronized boolean addAll(int index, Collection<? extends E> c) {
modCount++;
if (index < 0 || index > elementCount)
throw new ArrayIndexOutOfBoundsException(index);

Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityHelper(elementCount + numNew);

int numMoved = elementCount - index;
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);

System.arraycopy(a, 0, elementData, index, numNew);
elementCount += numNew;
return numNew != 0;
}

/**
* Compares the specified Object with this Vector for equality. Returns
* true if and only if the specified Object is also a List, both Lists
* have the same size, and all corresponding pairs of elements in the two
* Lists are <em>equal</em>. (Two elements {@code e1} and
* {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null :
* e1.equals(e2))}.) In other words, two Lists are defined to be
* equal if they contain the same elements in the same order.
*
* @param o the Object to be compared for equality with this Vector
* @return true if the specified Object is equal to this Vector
*/
public synchronized boolean equals(Object o) {
return super.equals(o);
}

/**
* Returns the hash code value for this Vector.
*/
public synchronized int hashCode() {
return super.hashCode();
}

/**
* Returns a string representation of this Vector, containing
* the String representation of each element.
*/
public synchronized String toString() {
return super.toString();
}

posted @ 2018-04-02 10:50  渴望成为大神的菜鸟  阅读(245)  评论(0编辑  收藏  举报