Vector源码解析

Posted buhuiflydepig

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Vector源码解析相关的知识,希望对你有一定的参考价值。

1、属性

//存储元素的数组
protected Object[] elementData;

//存储元素的个数
protected int elementCount;

//扩容时的增加量,大于0是增加capacityIncrement,否则增加两倍(默认两倍)
protected int capacityIncrement;

//数组的最大容量
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

 

2、构造方法

    //无参构造方法
    public Vector() {
     //默认初始容量为10
        this(10);
    }
    public Vector(int initialCapacity) {
        //默认扩容增量为0,即增加两倍
        this(initialCapacity, 0);
    }
    public Vector(int initialCapacity, int capacityIncrement) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
        this.capacityIncrement = capacityIncrement;
    }
    //不常用
    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);
    }

 

 

注意:下面的增删查改都只说明一个,其他的和ArrayList的操作差不多(只有扩容方式和ArrayList有区别)

3、添加

  public synchronized boolean add(E e) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = e;
        return true;
    }
private void ensureCapacityHelper(int minCapacity) {
        //如果实际数组不满足要求,则进行扩容
        if (minCapacity - elementData.length > 0)
            grow(minCapacity);
    }
    //扩容方法
    private void grow(int minCapacity) {
        int oldCapacity = elementData.length;
     //如果capacityIncrement大于0,扩容capacityIncrement,否则扩容两倍
        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;
    }

 

 

4、删除

    public boolean remove(Object o) {
        return removeElement(o);
    }
  public synchronized boolean removeElement(Object obj) {
        modCount++;
     //获取数组第一个元素为obj的下标
        int i = indexOf(obj);
        if (i >= 0) {
        //删除该元素
            removeElementAt(i);
            return true;
        }
        return false;
    }
    public int indexOf(Object o) {
        return indexOf(o, 0);
    }
    public synchronized int indexOf(Object o, int index) {
        if (o == null) {
            for (int i = index ; i < elementCount ; i++)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = index ; i < elementCount ; i++)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }
  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; /* to let gc do its work */
    }

 

//删除数组中下标为index的元素
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;
    }

 

 

5、查找

    public synchronized E get(int index) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);

        return elementData(index);
    }
    E elementData(int index) {
        return (E) elementData[index];
    }

 

 

6、修改

    public synchronized E set(int index, E element) {
        if (index >= elementCount)
            throw new ArrayIndexOutOfBoundsException(index);

        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }

 

以上是关于Vector源码解析的主要内容,如果未能解决你的问题,请参考以下文章

Java 集合系列06之 Vector详细介绍(源码解析)和使用示例

Java 集合Vector源码深入解析

Java集合类源码解析:Vector

Vector源码解析

anki_vector SDK源码解析(教程)

Vector源码解析(jdk1.8)