标签:
Vector是List接口的另一实现,有非常长的历史了,从jdk1.0开始就有Vector了,先于ArrayList出现,与ArrayList的最大区别是:Vector 是线程安全的,简单浏览一下Vector:
类定义:
1 public class Vector<E> 2 extends AbstractList<E> 3 implements List<E>, RandomAccess, Cloneable, java.io.Serializable
Vector支持快速随机访问,和arrayList一样。
Vector 初始容量为10,支持自定义每次扩容长度,如果不自定义扩容长度,那么默认扩容后的长度是原来的2倍。
1 /** 2 * Constructs an empty vector with the specified initial capacity and 3 * capacity increment. 4 * 5 * @param initialCapacity the initial capacity of the vector 6 * @param capacityIncrement the amount by which the capacity is 7 * increased when the vector overflows 8 * @throws IllegalArgumentException if the specified initial capacity 9 * is negative 10 */ 11 public Vector(int initialCapacity, int capacityIncrement) { 12 super(); 13 if (initialCapacity < 0) 14 throw new IllegalArgumentException("Illegal Capacity: "+ 15 initialCapacity); 16 this.elementData = new Object[initialCapacity]; 17 this.capacityIncrement = capacityIncrement; 18 } 19 20 /** 21 * Constructs an empty vector with the specified initial capacity and 22 * with its capacity increment equal to zero. 23 * 24 * @param initialCapacity the initial capacity of the vector 25 * @throws IllegalArgumentException if the specified initial capacity 26 * is negative 27 */ 28 public Vector(int initialCapacity) { 29 this(initialCapacity, 0); 30 } 31 32 /** 33 * Constructs an empty vector so that its internal data array 34 * has size {@code 10} and its standard capacity increment is 35 * zero. 36 */ 37 public Vector() { 38 this(10); 39 }
Vector最大的特点是Vector内部基本上都使用了synchronized进行同步或者调用使用了 synchronized同步关键字的方法:
1 /** 2 * Returns {@code true} if this vector contains the specified element. 3 * More formally, returns {@code true} if and only if this vector 4 * contains at least one element {@code e} such that 5 * <tt>(o==null ? e==null : o.equals(e))</tt>. 6 * 7 * @param o element whose presence in this vector is to be tested 8 * @return {@code true} if this vector contains the specified element 9 */ 10 public boolean contains(Object o) { 11 return indexOf(o, 0) >= 0; 12 } 13 14 /** 15 * Returns the index of the first occurrence of the specified element 16 * in this vector, or -1 if this vector does not contain the element. 17 * More formally, returns the lowest index {@code i} such that 18 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, 19 * or -1 if there is no such index. 20 * 21 * @param o element to search for 22 * @return the index of the first occurrence of the specified element in 23 * this vector, or -1 if this vector does not contain the element 24 */ 25 public int indexOf(Object o) { 26 return indexOf(o, 0); 27 } 28 29 /** 30 * Returns the index of the first occurrence of the specified element in 31 * this vector, searching forwards from {@code index}, or returns -1 if 32 * the element is not found. 33 * More formally, returns the lowest index {@code i} such that 34 * <tt>(i >= index && (o==null ? get(i)==null : o.equals(get(i))))</tt>, 35 * or -1 if there is no such index. 36 * 37 * @param o element to search for 38 * @param index index to start searching from 39 * @return the index of the first occurrence of the element in 40 * this vector at position {@code index} or later in the vector; 41 * {@code -1} if the element is not found. 42 * @throws IndexOutOfBoundsException if the specified index is negative 43 * @see Object#equals(Object) 44 */ 45 public synchronized int indexOf(Object o, int index) { 46 if (o == null) { 47 for (int i = index ; i < elementCount ; i++) 48 if (elementData[i]==null) 49 return i; 50 } else { 51 for (int i = index ; i < elementCount ; i++) 52 if (o.equals(elementData[i])) 53 return i; 54 } 55 return -1; 56 } 57 58 /** 59 * Returns the index of the last occurrence of the specified element 60 * in this vector, or -1 if this vector does not contain the element. 61 * More formally, returns the highest index {@code i} such that 62 * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, 63 * or -1 if there is no such index. 64 * 65 * @param o element to search for 66 * @return the index of the last occurrence of the specified element in 67 * this vector, or -1 if this vector does not contain the element 68 */ 69 public synchronized int lastIndexOf(Object o) { 70 return lastIndexOf(o, elementCount-1); 71 } 72 73 /** 74 * Returns the index of the last occurrence of the specified element in 75 * this vector, searching backwards from {@code index}, or returns -1 if 76 * the element is not found. 77 * More formally, returns the highest index {@code i} such that 78 * <tt>(i <= index && (o==null ? get(i)==null : o.equals(get(i))))</tt>, 79 * or -1 if there is no such index. 80 * 81 * @param o element to search for 82 * @param index index to start searching backwards from 83 * @return the index of the last occurrence of the element at position 84 * less than or equal to {@code index} in this vector; 85 * -1 if the element is not found. 86 * @throws IndexOutOfBoundsException if the specified index is greater 87 * than or equal to the current size of this vector 88 */ 89 public synchronized int lastIndexOf(Object o, int index) { 90 if (index >= elementCount) 91 throw new IndexOutOfBoundsException(index + " >= "+ elementCount); 92 93 if (o == null) { 94 for (int i = index; i >= 0; i--) 95 if (elementData[i]==null) 96 return i; 97 } else { 98 for (int i = index; i >= 0; i--) 99 if (o.equals(elementData[i])) 100 return i; 101 } 102 return -1; 103 }
Vector 的实现实际上跟ArrayList差别不大,ArrayList可参考 java1.7集合源码阅读:ArrayList 。
标签:
原文地址:http://www.cnblogs.com/jessezeng/p/5641596.html