Vector源码分析

通过IDEA生成Vector类图。框架结构与ArrayList一致![在这里插入图片描述](https://img-blog.csdnimg.cn/202012162在这里插入图片描述

Vector是同样是基于数组实现动态分配的list。可以动态扩容。但是是线程安全,通过方法加上synchronized保证线程安全。elementCount等同于ArrayList中的size,capacityIncrement可以指定扩容的步长。

    /**
     * The array buffer into which the components of the vector are
     * stored. The capacity of the vector is the length of this array buffer,
     * and is at least large enough to contain all the vector's elements.
     *
     * <p>Any array elements following the last element in the Vector are null.
     *
     * @serial
     */
    protected Object[] elementData;

    /**
     * The number of valid components in this {@code Vector} object.
     * Components {@code elementData[0]} through
     * {@code elementData[elementCount-1]} are the actual items.
     *
     * @serial
     */
    protected int elementCount;

    /**
     * The amount by which the capacity of the vector is automatically
     * incremented when its size becomes greater than its capacity.  If
     * the capacity increment is less than or equal to zero, the capacity
     * of the vector is doubled each time it needs to grow.
     *
     * @serial
     */
    protected int capacityIncrement;

Vector提供四种构造函数。Vector(int initialCapacity, int capacityIncrement) 、Vector(int initialCapacity)、Vector()、 Vector(Collection<? extends E> c)。

Vector初始容量是通过构造函数指定。
Vector(): 10个元素数组。
Vector(int initialCapacity): initialCapacity长度的数组,
Vector(Collection<? extends E> c): c.length长度的数组

动态扩容:

提供两种方式扩容。public synchronized void ensureCapacity(int minCapacity)、private void ensureCapacityHelper(int minCapacity)
ensureCapacity(int minCapacity): 外部指定容量。
ensureCapacityHelper(int minCapacity): 内部add、addAll、setSize、insertElementAt、addElement调用

扩容的代码同样是发生在grow(int minCapacity)。
按照 int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
capacityIncrement : oldCapacity);如果指定capacityIncrement,扩容capacityIncrement大小。如果capacityIncrement<=0,容量翻倍。
当扩容需求Integer.MAX_VALUE,如果无法满足minCapacity,则发生溢出。minCapacity大于MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8,返回Integer.MAX_VALUE(下次add就溢出了)。否则返回Integer.MAX_VALUE - 8

    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        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);
    }

    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

动态缩容:

容量不会缩小,但会把最后元素置为null,被垃圾回收掉。从而释放内存。

/**
 * Removes the element at the specified position in this Vector.
 * Shifts any subsequent elements to the left (subtracts one from their
 * indices).  Returns the element that was removed from the Vector.
 *
 * @throws ArrayIndexOutOfBoundsException if the index is out of range
 *         ({@code index < 0 || index >= size()})
 * @param index the index of the element to be removed
 * @return element that was removed
 * @since 1.2
 */
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值