jdk8源码4---集合3---Vector

一、签名

public class Vector<E>

    extends AbstractList<E>

    implements List<E>, RandomAccess, Cloneable, java.io.Serializable

 

二、成员变量

Protect Object[] elementData;

Protect int elementCount;

Protect int capacitIncrement;

 

 

三、构造方法

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

    }

public Vector() {

        this(10);  //vector的默认初始化大小为10

    }

 

四、成员方法

Vector和ArrayList差不多。不同的是Vector的方法上加上了synchronized关键字。这表明Vector的线程同步的,线程安全的。

 

1.  add()

public synchronized void addElement(E obj) {

        modCount++;

        ensureCapacityHelper(elementCount + 1);

        elementData[elementCount++] = obj;

    }

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;

    }

扩容的算法:如果没有指定扩容大小,那么就默认扩容增量为当前的容量+1,则扩容后的大小为两倍的当前容量+1,称之为新容量。如果新容量小于需要的容量大小,那将这个需要的容量赋值给新容量,如果新容量大于最大的数组大小,那么扩容到最大,即MAX_ARRAY_SIZE。

public synchronized void insertElementAt(E obj, int index) {

        modCount++;

        if (index > elementCount) {

            throw new ArrayIndexOutOfBoundsException(index

                                                     + " > " + elementCount);

        }

        ensureCapacityHelper(elementCount + 1);

        System.arraycopy(elementData, index, elementData, index + 1, elementCount - index);

        elementData[index] = obj;

        elementCount++;

    }

关键还是在判断容量上。

 

2.  remove()

public synchronized boolean removeElement(Object obj) {

        modCount++;

        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 */

    }

 

3.  publicboolean contains(Object o)

public boolean contains(Object o) {

        return indexOf(o, 0) >= 0;

    }

indexOf(),见上。

4.   

public synchronized E firstElement() {

        if (elementCount == 0) {

            throw new NoSuchElementException();

        }

        return elementData(0);

    }

public synchronized E lastElement() {

        if (elementCount == 0) {

            throw new NoSuchElementException();

        }

        return elementData(elementCount - 1);

    }

 

 

五、遍历方式

public Enumeration<E> elements() {

        return new Enumeration<E>() {

            int count = 0;

            public boolean hasMoreElements() {

                return count < elementCount;

            }

            public E nextElement() {

                synchronized (Vector.this) {

                    if (count < elementCount) {

                        return elementData(count++);

                    }

                }

                throw new NoSuchElementException("Vector Enumeration");

            }

        };

    }

for

fori

Iterator

 

总结

    允许元素为空。

    有序

    线程安全

最后


如果你觉得写的还不错,就关注下公众号呗,关注后,有点小礼物回赠给你。
你可以获得5000+电子书,java,springCloud,adroid,python等各种视频教程,IT类经典书籍,各种软件的安装及破解教程。
希望一块学习,一块进步!

posted @ 2018-06-10 13:37  方家小白  阅读(18)  评论(0编辑  收藏  举报