【深入Java基础】Vector源码分析

Vector源码分析


Vector和ArrayList一样,基于数组实现。但是Vector实现了同步,里边有好多synchronized修饰的方法。所以适合在多线程下使用。

存放数据的数组

    protected Object[] elementData;

数组长度

    protected int elementCount;

增量

   protected int capacityIncrement;

构造方法

     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(int initialCapacity) {
        this(initialCapacity, 0);
    }

都不指定,默认容量为10的构造方法

    public Vector() {
        this(10);
    }

从数组复制

    public synchronized void copyInto(Object[] anArray) {
        System.arraycopy(elementData, 0, anArray, 0, elementCount);
    }

移除多余的增长空间

   public synchronized void trimToSize() {
    modCount++;
    int oldCapacity = elementData.length;
    if (elementCount < oldCapacity) {
    elementData = Arrays.copyOf(elementData, elementCount);
    }
    }

自动增长

这里的自动增长的增量为原来长度的一倍,而ArrayList则是原来的0.5倍

    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);
    }


    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;
    }

重置Vector大小

重新设置Vector的大小,如果新的容量小于原容量,则多余的元素将被置为null(舍弃);如果新容量大于原容量,则增长为新容量。

     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 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;
    }

返回索引处的元素

除下边这个外还有最常用的get方法,功能完全一样,只不过get(以及下边的增删查改方法)都有两个,一个是Vector的,还有一个是重写的父类List中的方法,功能完全一样。

    public synchronized E elementAt(int index) {
        if (index >= elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
        }

        return elementData(index);
    }

修改元素

     public synchronized void setElementAt(E obj, int index) {
        if (index >= elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                     elementCount);
        }
        elementData[index] = obj;
    }

添加元素

     public synchronized void addElement(E obj) {
        modCount++;
        ensureCapacityHelper(elementCount + 1);
        elementData[elementCount++] = obj;
    }

删除元素

     public synchronized boolean removeElement(Object obj) {
        modCount++;
        int i = indexOf(obj);
        if (i >= 0) {
            removeElementAt(i);
            return true;
        }
        return false;
    }

……

原文地址:https://www.cnblogs.com/cnsec/p/13286737.html