Java集合-Vector源码解析-JDK1.8

Vector简介

与ArrayList不同的是,Vector是线程安全的。

建议先阅读 ArrayList源码分析 ,再回来看此文会Soeasy哦!

Vector继承了AbstractList实现了List, RandomAccess, Cloneable, java.io.Serializable这些接口。

  1. AbstractList、List提供了添加、删除、修改、遍历等功能。

  2. RandmoAccess提供了随机访问功能

  3. Cloneable提供了可以被克隆的功能

  4. 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完整源码解析请 点击下方“阅读原文”查看!!!

640?wx_fmt=jpeg

万水千山总是情,点个 “在看” 行不行!!!

640?wx_fmt=png 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值