Vector源码解析

Vector源码解析

Vector结构类图

在这里插入图片描述

Vector成员属性
//数组元素
protected Object[] elementData;

//数组元素个数
protected int elementCount;

//增长系数
protected int capacityIncrement;

//序列号
private static final long serialVersionUID = -2767605614048989439L;
Vector构造方法
//指定数组初始容量大小和增长系数
public Vector(int initialCapacity, int capacityIncrement) {
    super();
    //初始容量<0,抛出异常
    if (initialCapacity < 0)
        throw new IllegalArgumentException("Illegal Capacity: "+
                                           initialCapacity);
    //为对象数组指定初始化容量
    this.elementData = new Object[initialCapacity];
    //设置增长系数
    this.capacityIncrement = capacityIncrement;
}
//指定Vector初始化容量大小,默认增长系数为0
public Vector(int initialCapacity) {
    this(initialCapacity, 0);
}
//构造一个空的Vector,初始化容量为10,增长系数为0
public Vector() {
    this(10);
}
//构造一个包含指定集合C中的元素的数组
public Vector(Collection<? extends E> c) {
    //获取集合c中的元素,将其放入数组中
    Object[] a = c.toArray();
    //Vector的元素个数就是数组的长度
    elementCount = a.length;
    //如果集合c的类型是ArrayList类型,直接赋值给elementData
    if (c.getClass() == ArrayList.class) {
        elementData = a;
    //否则进行浅拷贝将类型转化为对象数组
    } else {
        elementData = Arrays.copyOf(a, elementCount, Object[].class);
    }
}
Vector主要方法
add()方法
//synchronized表示方法是同步的
public synchronized boolean add(E e) {
    modCount++;
    //传入最小需要的容量,确定Vector容量
    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;
    	//如果有给capacityIncrement设置增长系数的话,就加上该系数值来扩容,否则将原先的数组容量变为2*oldCapacity
        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);
    }
//在指定位置插入元素
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);
        }
    	//传入最小需要的容量,确定Vector容量
        ensureCapacityHelper(elementCount + 1);
    	//复制数组
        System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);
    	//插入元素
        elementData[index] = obj;
        elementCount++;
    }
get()方法
//获取指定索引位置的元素
public synchronized E get(int index) {
    //如果索引位置大于等于数组元素个数,则抛出异常
    if (index >= elementCount)
        throw new ArrayIndexOutOfBoundsException(index);
	
    return elementData(index);
}

E elementData(int index) {
    	//直接从数组下标获取元素
        return (E) elementData[index];
    }
set()方法
//将指定索引位置的元素替换成传入元素
public synchronized E set(int index, E element) {
    //索引检查
    if (index >= elementCount)
        throw new ArrayIndexOutOfBoundsException(index);
	//获取旧值
    E oldValue = elementData(index);
    //赋新值
    elementData[index] = element;
    //返回旧值
    return oldValue;
}
remove()方法
//删除指定位置的元素
public synchronized E remove(int index) {
    modCount++;
    //索引检查
    if (index >= elementCount)
        throw new ArrayIndexOutOfBoundsException(index);
    //获取旧值
    E oldValue = elementData(index);
	//计算将要移动的元素个数
    int numMoved = elementCount - index - 1;
    //如果大于0,则移动
    if (numMoved > 0)
        //元素移动
        System.arraycopy(elementData, index+1, elementData, index,
                         numMoved);
    //让最后位置元素赋null
    elementData[--elementCount] = null; // Let gc do its work
	//返回旧值
    return oldValue;
}
//删除指定元素
public boolean remove(Object o) {
    return removeElement(o);
}

public synchronized boolean removeElement(Object obj) {
        modCount++;
    	//获取当前元素的索引
        int i = indexOf(obj);
    	//索引大于0,则调用removeElementAt(i);删除
        if (i >= 0) {
            removeElementAt(i);
            return true;
        }
        return false;
    }

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;
    	//如果大于0,则移动
        if (j > 0) {
            //元素移动
            System.arraycopy(elementData, index + 1, elementData, index, j);
        }
        elementCount--;
    	//让最后位置元素赋null
        elementData[elementCount] = null; /* to let gc do its work */
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值