标签:ons into null extends news sdn word [] 计数
转载请注明出处:http://blog.csdn.net/ns_code/article/details/35793865
Vector简介
Vector也是基于数组实现的,是一个动态数组,其容量能自动增长。
Vector是JDK1.0引入了,它的很多实现方法都加入了同步语句,因此是线程安全的(其实也只是相对安全,有些时候还是要加入同步语句来保证线程的安全),可以用于多线程环境。
Vector没有丝线Serializable接口,因此它不支持序列化,实现了Cloneable接口,能被克隆,实现了RandomAccess接口,支持快速随机访问。
Vector源码剖析
Vector的源码如下(加入了比较详细的注释):
- package java.util;
-
- public class Vector<E>
- extends AbstractList<E>
- implements List<E>, RandomAccess, Cloneable, java.io.Serializable
- {
-
-
- protected Object[] elementData;
-
-
- protected int elementCount;
-
-
- protected int capacityIncrement;
-
-
- private static final long serialVersionUID = -2767605614048989439L;
-
-
- public Vector() {
- this(10);
- }
-
-
- 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;
-
- if (elementData.getClass() != Object[].class)
- elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
- }
-
-
- public synchronized void copyInto(Object[] anArray) {
- System.arraycopy(elementData, 0, anArray, 0, elementCount);
- }
-
-
- public synchronized void trimToSize() {
- modCount++;
- int oldCapacity = elementData.length;
- if (elementCount < oldCapacity) {
- elementData = Arrays.copyOf(elementData, elementCount);
- }
- }
-
-
- private void ensureCapacityHelper(int minCapacity) {
- int oldCapacity = elementData.length;
-
-
-
- if (minCapacity > oldCapacity) {
- Object[] oldData = elementData;
- int newCapacity = (capacityIncrement > 0) ?
- (oldCapacity + capacityIncrement) : (oldCapacity * 2);
- if (newCapacity < minCapacity) {
- newCapacity = minCapacity;
- }
- elementData = Arrays.copyOf(elementData, newCapacity);
- }
- }
-
-
- public synchronized void ensureCapacity(int minCapacity) {
-
- modCount++;
- ensureCapacityHelper(minCapacity);
- }
-
-
- public synchronized void setSize(int newSize) {
- modCount++;
- if (newSize > elementCount) {
-
- ensureCapacityHelper(newSize);
- } else {
-
- for (int i = newSize ; i < elementCount ; i++) {
- elementData[i] = null;
- }
- }
- elementCount = newSize;
- }
-
-
- public synchronized int capacity() {
- return elementData.length;
- }
-
-
- public synchronized int size() {
- return elementCount;
- }
-
-
- public synchronized boolean isEmpty() {
- return elementCount == 0;
- }
-
-
- 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 (E)elementData[count++];
- }
- }
- throw new NoSuchElementException("Vector Enumeration");
- }
- };
- }
-
-
- public boolean contains(Object o) {
- return indexOf(o, 0) >= 0;
- }
-
-
-
-
- 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;
- }
-
-
- public int indexOf(Object o) {
- return indexOf(o, 0);
- }
-
-
- public synchronized int lastIndexOf(Object o) {
- return lastIndexOf(o, elementCount-1);
- }
-
-
-
- public synchronized int lastIndexOf(Object o, int index) {
- if (index >= elementCount)
- throw new IndexOutOfBoundsException(index + " >= "+ elementCount);
-
- if (o == null) {
-
- for (int i = index; i >= 0; i--)
- if (elementData[i]==null)
- return i;
- } else {
-
- for (int i = index; i >= 0; i--)
- if (o.equals(elementData[i]))
- return i;
- }
- return -1;
- }
-
-
-
- public synchronized E elementAt(int index) {
- if (index >= elementCount) {
- throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
- }
-
- return (E)elementData[index];
- }
-
-
-
- public synchronized E firstElement() {
- if (elementCount == 0) {
- throw new NoSuchElementException();
- }
- return (E)elementData[0];
- }
-
-
-
- public synchronized E lastElement() {
- if (elementCount == 0) {
- throw new NoSuchElementException();
- }
- return (E)elementData[elementCount - 1];
- }
-
-
- public synchronized void setElementAt(E obj, int index) {
- if (index >= elementCount) {
- throw new ArrayIndexOutOfBoundsException(index + " >= " +
- elementCount);
- }
- elementData[index] = obj;
- }
-
-
- public synchronized void removeElementAt(int index) {
- modCount++;
- if (index >= elementCount) {
- throw new ArrayIndexOutOfBoundsException(index + " >= " +
- elementCount);
- } else if (index < 0) {
- throw new ArrayIndexOutOfBoundsException(index);
- }
-
- int j = elementCount - index - 1;
- if (j > 0) {
- System.arraycopy(elementData, index + 1, elementData, index, j);
- }
- elementCount--;
- elementData[elementCount] = null;
- }
-
-
- public synchronized void insertElementAt(E obj, int index) {
- modCount++;
- if (index > elementCount) {
- throw new ArrayIndexOutOfBoundsException(index
- + " > " + elementCount);
- }
- ensureCapacityHelper(elementCount + 1);
- System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
- elementData[index] = obj;
- elementCount++;
- }
-
-
- public synchronized void addElement(E obj) {
- modCount++;
- ensureCapacityHelper(elementCount + 1);
- elementData[elementCount++] = obj;
- }
-
-
-
- public synchronized boolean removeElement(Object obj) {
- modCount++;
- int i = indexOf(obj);
- if (i >= 0) {
- removeElementAt(i);
- return true;
- }
- return false;
- }
-
-
- public synchronized void removeAllElements() {
- modCount++;
-
- for (int i = 0; i < elementCount; i++)
- elementData[i] = null;
-
- elementCount = 0;
- }
-
-
- public synchronized Object clone() {
- try {
- Vector<E> v = (Vector<E>) super.clone();
-
- v.elementData = Arrays.copyOf(elementData, elementCount);
- v.modCount = 0;
- return v;
- } catch (CloneNotSupportedException e) {
-
- throw new InternalError();
- }
- }
-
-
- public synchronized Object[] toArray() {
- return Arrays.copyOf(elementData, elementCount);
- }
-
-
- public synchronized <T> T[] toArray(T[] a) {
-
-
- if (a.length < elementCount)
- return (T[]) Arrays.copyOf(elementData, elementCount, a.getClass());
-
-
-
- System.arraycopy(elementData, 0, a, 0, elementCount);
-
- if (a.length > elementCount)
- a[elementCount] = null;
-
- return a;
- }
-
-
- public synchronized E get(int index) {
- if (index >= elementCount)
- throw new ArrayIndexOutOfBoundsException(index);
-
- return (E)elementData[index];
- }
-
-
- public synchronized E set(int index, E element) {
- if (index >= elementCount)
- throw new ArrayIndexOutOfBoundsException(index);
-
- Object oldValue = elementData[index];
- elementData[index] = element;
- return (E)oldValue;
- }
-
-
- public synchronized boolean add(E e) {
- modCount++;
- ensureCapacityHelper(elementCount + 1);
- elementData[elementCount++] = e;
- return true;
- }
-
-
- public boolean remove(Object o) {
- return removeElement(o);
- }
-
-
- public void add(int index, E element) {
- insertElementAt(element, index);
- }
-
-
- public synchronized E remove(int index) {
- modCount++;
- if (index >= elementCount)
- throw new ArrayIndexOutOfBoundsException(index);
- Object oldValue = elementData[index];
-
- int numMoved = elementCount - index - 1;
- if (numMoved > 0)
- System.arraycopy(elementData, index+1, elementData, index,
- numMoved);
- elementData[--elementCount] = null;
-
- return (E)oldValue;
- }
-
-
- public void clear() {
- removeAllElements();
- }
-
-
- public synchronized boolean containsAll(Collection<?> c) {
- return super.containsAll(c);
- }
-
-
- public synchronized boolean addAll(Collection<? extends E> c) {
- modCount++;
- Object[] a = c.toArray();
- int numNew = a.length;
- ensureCapacityHelper(elementCount + numNew);
-
- System.arraycopy(a, 0, elementData, elementCount, numNew);
- elementCount += numNew;
- return numNew != 0;
- }
-
-
- public synchronized boolean removeAll(Collection<?> c) {
- return super.removeAll(c);
- }
-
-
- public synchronized boolean retainAll(Collection<?> c) {
- return super.retainAll(c);
- }
-
-
- 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;
- }
-
-
- public synchronized boolean equals(Object o) {
- return super.equals(o);
- }
-
-
- public synchronized int hashCode() {
- return super.hashCode();
- }
-
-
- public synchronized String toString() {
- return super.toString();
- }
-
-
- public synchronized List<E> subList(int fromIndex, int toIndex) {
- return Collections.synchronizedList(super.subList(fromIndex, toIndex), this);
- }
-
-
- protected synchronized void removeRange(int fromIndex, int toIndex) {
- modCount++;
- int numMoved = elementCount - toIndex;
- System.arraycopy(elementData, toIndex, elementData, fromIndex,
- numMoved);
-
-
- int newElementCount = elementCount - (toIndex-fromIndex);
- while (elementCount != newElementCount)
- elementData[--elementCount] = null;
- }
-
-
- private synchronized void writeObject(java.io.ObjectOutputStream s)
- throws java.io.IOException {
- s.defaultWriteObject();
- }
- }
几点总结
Vector的源码实现总体与ArrayList类似,关于Vector的源码,给出如下几点总结:
1、Vector有四个不同的构造方法。无参构造方法的容量为默认值10,仅包含容量的构造方法则将容量增长量(从源码中可以看出容量增长量的作用,第二点也会对容量增长量详细说)明置为0。
2、注意扩充容量的方法ensureCapacityHelper。与ArrayList相同,Vector在每次增加元素(可能是1个,也可能是一组)时,都要调用该方法来确保足够的容量。当容量不足以容纳当前的元素个数时,就先看构造方法中传入的容量增长量参数CapacityIncrement是否为0,如果不为0,就设置新的容量为就容量加上容量增长量,如果为0,就设置新的容量为旧的容量的2倍,如果设置后的新容量还不够,则直接新容量设置为传入的参数(也就是所需的容量),而后同样用Arrays.copyof()方法将元素拷贝到新的数组。
3、很多方法都加入了synchronized同步语句,来保证线程安全。
4、同样在查找给定元素索引值等的方法中,源码都将该元素的值分为null和不为null两种情况处理,Vector中也允许元素为null。 5、其他很多地方都与ArrayList实现大同小异,Vector现在已经基本不再使用。
转:【Java集合源码剖析】Vector源码剖析
标签:ons into null extends news sdn word [] 计数
原文地址:http://www.cnblogs.com/xuyatao/p/6916978.html