Java集合学习四 Vector

一 Vector继承关系

可以看出   Vector类继承了抽象AbstractList类 并且也实现了List接口  , Serializable ,RandomAccess ,Cloneable 接口。

那就先看看之前没有详细看过的接口 ,先看下Serializable接口,再看下Cloneable接口。

二 Serializable接口

引用文章:https://blog.csdn.net/u013034889/article/details/61919232

1.同样的,看源码知道,这是一个标记接口。

2.当一个对象要被序列化,这个对象所属的类必须实现Serializable接口。

3.当反序列化对象时,该接口给序列化的类,提供了一个序列版本号。serialVersionUID. 该版本号的目的在于验证序列化的对象和对应类是否版本匹配。

三 Cloneable 接口

引用文章:https://blog.csdn.net/qq_37113604/article/details/81168224

具体知道深度克隆和浅度克隆的区别和使用方法。

四 看完了接口再看下Vector内部的结构

 

除了继承实现了之前看过的接口和抽象类的方法外,本身的方法也有很多,而且自身的数据结构是数组型的,还有几个变量,慢慢看。

五 内部变量

protected Object[] elementData;也就是Vector内部实现是以Object数组的方式实现的,
protected int elementCount; 元素个数
protected int capacityIncrement;当矢量的大小大于其容量时,矢量的容量自动增加的量

六 内部方法

public Vector() {
        this(10); //默认无参构造初始化为10的容量大小
    }

public Vector(int initialCapacity) {
        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);//强制放回Object[]类型的数组
    }

//修改该向量的容量成为向量的当前大小。 如果该向量的容量大于其当前大小,则通过用保留在字段elementData中的较小的内部数据阵列替换其内部数据阵列,将容量更改为等于大小。 应用程序可以使用此操作来最小化向量的存储。
public synchronized void copyInto(Object[] anArray) {
        System.arraycopy(elementData, 0, anArray, 0, 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);
    }

//真正的扩容方法  
private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;       //原来容量大小
        int newCapacity = oldCapacity + ((capacityIncrement > 0) ?  假如容量增量大于零的话,则进行与原来容量大小相加后作为新容量,假如容量增量小于等于0的话,则进行扩容一倍的操作,作为新容量大小。
                                         capacityIncrement : oldCapacity);
        if (newCapacity - minCapacity < 0) //如果新容量还是小于我们希望Vector的大小,则直接将我们期望的容量作为新容量。
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)//如果新容量非常大则进行hugeCapacity方法
            newCapacity = hugeCapacity(minCapacity);
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

private static int hugeCapacity(int minCapacity) { 
        if (minCapacity < 0) // overflow//如果期望容量是负数,则抛出OOM异常
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

public synchronized int capacity() {
        return elementData.length;  //容量大小
    }

public synchronized int size() {
        return elementCount; //有效元素大小
    }


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

//从指定index位置开始搜索第一个出现Object的索引值
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;
    }

//从指定index位置开始倒置搜索第一个出现Object的索引值
public synchronized int lastIndexOf(Object o, int index) {
        if (index >= elementCount)
            throw new IndexOutOfBoundsException(index + " >= "+ elementCount);

        if (o == null) {
            for (int i = index; i >= 0; i--)
                if (elementData[i]==null)
                    return i;
        } else {
            for (int i = index; i >= 0; i--)
                if (o.equals(elementData[i]))
                    return i;
        }
        return -1;
    }

//得到指定索引位置的元素
public synchronized E elementAt(int index) {
        if (index >= elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
        }

        return elementData(index);
    }

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 synchronized void setElementAt(E obj, int index) {
        if (index >= elementCount) {
            throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                     elementCount);
        }
        elementData[index] = obj;
    }

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

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

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;
    }
public synchronized void removeAllElements() {
        modCount++;
        // Let gc do its work
        for (int i = 0; i < elementCount; i++)
            elementData[i] = null;

        elementCount = 0;
    }

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

将Vector中的元素以OOS写出
private void writeObject(java.io.ObjectOutputStream s)
            throws java.io.IOException {
        final java.io.ObjectOutputStream.PutField fields = s.putFields();
        final Object[] data;
        synchronized (this) {
            fields.put("capacityIncrement", capacityIncrement);
            fields.put("elementCount", elementCount);
            data = elementData.clone();
        }
        fields.put("elementData", data);
        s.writeFields();
    }

总结:

Vector是同步的。由Vector创建的Iterator,虽然和ArrayList创建的Iterator是同一接口,但是,因为Vector是同步的,当一个Iterator被创建而且正在被使用,另一个线程改变了Vector的状态(例如,添加或删除了一些元素),这时调用Iterator的方法时将抛出ConcurrentModificationException,因此必须捕获该异常。

Vector实现List接口,所以里面的元素是有序的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值