重学List之Vector源码分析

mac2025-08-15  2

1.简介

继承AbstractList,实现List接口,继承添加,修改,遍历,包含等父类方法。实现RandmoAccess接口,标记性接口,即Vector是支持快速随机访问的。实现Cloneable接口,可以被克隆。实现java.io.Serializable接口,支持序列化。

2.成员属性

//存储数据的数组 protected Object[] elementData; //数组存储的元素数量 protected int elementCount; //扩容时增加的容量 protected int capacityIncrement;

3.构造函数

/** * 初始化数组长度为10 */ public Vector(int initialCapacity, int capacityIncrement) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; this.capacityIncrement = capacityIncrement; } /** * 自增容量是0 */ public Vector(int initialCapacity) { this(initialCapacity, 0); } /** * 数组长度为10 */ public Vector() { this(10); } /** * 也支持从其他容器构造 */ 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); }

4.方法分析

synchronized boolean add(E e)

public synchronized boolean add(E e) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = e; return true; } 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; //扩容oldCapacity+oldCapacity 即:扩容为原来的2倍 int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); } //大容量扩容,最大支持到int的最大值 private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; }

void add(int index, E element)

public void add(int index, E element) { insertElementAt(element, 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++; }

synchronized E get(int index)

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]; }

synchronized E remove(int index)

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; }

boolean remove(Object o)

public boolean remove(Object o) { return removeElement(o); } public synchronized boolean removeElement(Object obj) { modCount++; int i = indexOf(obj); if (i >= 0) { removeElementAt(i); return true; } return false; } public int indexOf(Object o) { return indexOf(o, 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 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; }

5.总结

Vector底层也是数组。存储的元素是有序的。存储元素可以重复。随机访问效率高,增删效率低。通过索引可以很快的查找到对应元素,而增删元素许多元素的位置都要改变。线程安全,通过synchronized加锁实现同步。
最新回复(0)