◆
Vector简介
◆
与ArrayList不同的是,Vector是线程安全的。
建议先阅读 ArrayList源码分析 ,再回来看此文会Soeasy哦!
Vector继承了AbstractList实现了List, RandomAccess, Cloneable, java.io.Serializable这些接口。
◆
Vector的属性
◆
/** * 真实存储Vector元素的数组缓冲区 */ protected Object[] elementData;
/** * Vector的实际元素数量 */ protected int elementCount;
/** * 容量增长系数 */ protected int capacityIncrement; /** * Vector能够增长到的最大容量 */ private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
◆
Vector的构造方法
◆
/** * 指定Vector的初始大小以及容量增长系数 */ public Vector(int initialCapacity, int capacityIncrement) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity); this.elementData = new Object[initialCapacity]; this.capacityIncrement = capacityIncrement; }
/** * 指定Vector的初始大小 */ public Vector(int initialCapacity) { this(initialCapacity, 0); }
/** * Vector的默认初始大小为10 */ public Vector() { this(10); }
/** * 将集合的数据转换成Vector */ public Vector(Collection<? extends E> c) { elementData = c.toArray(); elementCount = elementData.length; // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, elementCount, Object[].class); }
◆
Vector添加元素
◆
/** * 添加方法 */ public synchronized boolean add(E e) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = e; return true; } /** * 指定索引添加元素 */ public void add(int index, E element) { insertElementAt(element, index); } /** * 在index索引位置插入数据 */ 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; }
可以看的,这几个添加方法最终都被synchronize关键字所修饰包括查询、修改和删除),只有这样才能保证线程安全。
◆
Vector扩容
◆
相信细心的你一定发现了上述的添加方法都调用了一个方法ensureCapacityHelper,这个方法就是用来确认Vector的容量的方法
/** * 确认Vector的容量 */ private void ensureCapacityHelper(int minCapacity) { // overflow-conscious code if (minCapacity - elementData.length > 0) //调用扩容方法 grow(minCapacity); } //扩容 private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; //如果指定了容量增长系数则按照capacityIncrement扩容,否则增大一倍 int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); //如果扩容后还不能存下元素,则使用minCapacity作为最新的容量 if (newCapacity - minCapacity < 0) newCapacity = minCapacity; //如果超出最大容量 if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); }
private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; }
◆
Vector的查询和修改
◆
修改和查询方法都比较简单
/** * 更新 */ public synchronized E set(int index, E element) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index); elementData[index] = element; return oldValue; } public synchronized E get(int index) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index);
return elementData(index); } @SuppressWarnings("unchecked") E elementData(int index) { return (E) elementData[index]; }
◆
Vector的删除
◆
删除涉及到了一个数组copy的过程
/** * 删除指定索引位置的元素 */ public synchronized E remove(int index) { modCount++; if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); E oldValue = elementData(index);
int numMoved = elementCount - index - 1; if (numMoved > 0) System.arraycopy(elementData, index + 1, elementData, index, numMoved); elementData[--elementCount] = null; // Let gc do its work
return oldValue; }
鉴于篇幅有限,本篇文章仅列出上方部分代码,Vector完整源码解析请点击下方“阅读原文”查看!!!