Vector 源码解析(jdk1.8)

Vector 是 Java 提供的可以自动增长的动态数组,同时是线程安全的。
Java的继承体系:
在这里插入图片描述

  1. 成员变量
// 存放元素的数组
 protected Object[] elementData;

// 元素个数
protected int elementCount;

//容器自动增大值
protected int capacityIncrement;
  1. 构造函数
public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
         // 初始数组,长度为initialCapacity
        this.elementData = new Object[initialCapacity];
        // 设置自动增长值
        this.capacityIncrement = capacityIncrement;
}
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);
    }
  1. 主要成员方法
// 把Vector的数组复制到指定的数组中
public synchronized void copyInto(Object[] anArray) {
        System.arraycopy(elementData, 0, anArray, 0, elementCount);
    }
// Trims the capacity of this vector to be the vector's current size.
// 把Vector的长度作为容器大小
public synchronized void trimToSize() {
// modCount表示修改的次数,在迭代器中使用
        modCount++;
        int oldCapacity = elementData.length;
        if (elementCount < oldCapacity) {
            elementData = Arrays.copyOf(elementData, elementCount);
        }
    }     
//  设置数组长度
public synchronized void ensureCapacity(int minCapacity) {
        if (minCapacity > 0) {
            modCount++;
            ensureCapacityHelper(minCapacity);
        }
    }
// 如果设置值大于数组长度,则增加数组长度到设置值
private void ensureCapacityHelper(int minCapacity) {
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
// 设置数组元素个数
public synchronized void setSize(int newSize) {
        modCount++;
        if (newSize > elementCount) {
            ensureCapacityHelper(newSize);
        } else {
            for (int i = newSize ; i < elementCount ; i++) {
                elementData[i] = null;
            }
        }
        elementCount = newSize;
    }
// 获取数组长度
public synchronized int capacity() {
        return elementData.length;
    }
// 获取数组元素个数
 public synchronized int size() {
        return elementCount;
  }
  // 判断数组是否为空
public synchronized boolean isEmpty() {
        return elementCount == 0;
  }
// 是否包含对象
public boolean contains(Object o) {
        return indexOf(o, 0) >= 0;
}
// 添加元素
public synchronized boolean add(E e) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = e;
        return true;
    } 
// 按索引获取元素
    public synchronized E get(int index) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);
// 
        return elementData(index);
    }
// 替代某个位置的元素
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 boolean removeElement(Object obj) {
        modCount++;
        int i = indexOf(obj);
        if (i >= 0) {
            removeElementAt(i);
            return true;
        }
        return false;
    }
// 移除全部元素
  public synchronized void removeAllElements() {
        modCount++;
        // Let gc do its work
        for (int i = 0; i < elementCount; i++)
            elementData[i] = null;

        elementCount = 0;
    }
// 克隆该对象
public synchronized Object clone() {
        try {
            @SuppressWarnings("unchecked")
                Vector<E> v = (Vector<E>) super.clone();
            v.elementData = Arrays.copyOf(elementData, elementCount);
            v.modCount = 0;
            return v;
        } catch (CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw new InternalError(e);
        }
    }    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值