Vector源码分析

1.简介

说实话Vector在工作中用的比较少,至少我没有用到过。Vector在面试中经常用来和ArrayList进行比较,但凡知道
Vector的都应该知道Vector是线程安全的,而ArrayList是线程不安全的,究其原因其实就是因为Vector在增、改、
删操方法加了synchronized,利用锁来保证线程安全。

2.Vector的继承关系

在这里插入图片描述
Vector也继承了AbstractList,实现了List、RandomAccess、Cloneable、Serializable等接口,和ArrayList继承关系是一样的。

3.构造函数

Vector类中变量:

	/*
	 * 真正存储元素的数组
	 */
 	protected Object[] elementData;
    /**
     * 数组中元素的真正个数
     */
    protected int elementCount;
    /**
     * 扩容时的增长系数,若为0,扩容时容量成倍增长
     * 若不为0,扩容后容量:原来容量+capacityIncrement
     */
    protected int capacityIncrement;
public Vector() {
		//默认容量为10
        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];
        //扩容因子默认为0
        this.capacityIncrement = capacityIncrement;
    }
  • new Vector()创建Vector对象,那么elementData的初始容量就是10,扩容因子capacityIncrement 则为0,扩容时的操作就是:elementData.length()+elementData.length()
  • new Vector(x, y)创建对象,那么elementData的初始容量就是x,因子capacityIncrement 则为y,扩容时的操作就是:elementData.length()+y

4.add方法

4.1 add(E e)

可以看到add方法被synchronized 修饰,锁就是当前对象(this)。
public synchronized boolean add(E e) {
		//1.modCount记录Vector变更的次数
        modCount++;
        //2.确定是否需要扩容
        ensureCapacityHelper(elementCount + 1);
        //3.添加元素
        elementData[elementCount++] = e;
        return true;
    }
private void ensureCapacityHelper(int minCapacity) {
        //和ArrayList处理相同
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
private void grow(int minCapacity) {
        int oldCapacity = elementData.length;
        //和ArrayList扩容方式不同,如果capacityIncrement为0,Vector的
        //容量就是成倍增加;如果capacityIncrement的值为x,vector的容量
        //是elementData.length+x
        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);
    }

4.2 addElement(E obj)

	其实操作和前面的add(E e)是一样的
public synchronized void addElement(E obj) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = obj;
    }

4.3 insertElementAt(E obj, int index)

	这里有一点需要注意,elementCount是Vector存储元素的个数。
public synchronized void insertElementAt(E obj, int index) {
        modCount++;
        //1.判断插入地方是否合理
        if (index > elementCount) {
            throw new ArrayIndexOutOfBoundsException(index
                                                     + " > " + elementCount);
        }
        //2.是否需要扩容
        ensureCapacityHelper(elementCount + 1);
        //3.直接将index之后的元素copy到elementData index之后的位置
        System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
        //4.插入元素
        elementData[index] = obj;
        elementCount++;
    }

5.get方法

 public synchronized E get(int index) {
 		//1.判断插入地方是否合理
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);
         //2.直接返回
        return elementData(index);
    }

6.set方法

有一点需要注意,修改elelmentData中某个元素时,modCount并没有自增。
public synchronized E set(int index, E element) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);
        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }

7.remove方法

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

8.总结

对于Vector的实现其实基本上和ArrayList一样的,只不过Vector是线程安全,而ArrayList是非线程安全的;Vector扩容方式和ArrayList的扩容方式不同,Vector有扩容因子,而ArrayList是直接1.5倍扩容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值