首页 > 编程语言 > 详细

java容器体系(四)----List(Vector)

时间:2020-09-11 09:38:04      阅读:83      评论:0      收藏:0      [点我收藏+]

  Vector 是 AbstractList 的直接子类之一,类声明的形式与 ArrayList 相同,同样实现了 RandomAccess、Cloneable 和 Serializable 接口,支持随机访问。它与ArrayList 的实现方式类似,不同的是,Verctor 是同步的,但是在性能上 Vector 处于劣势,因此在不需要考虑线程安全的情况下,尽量还是使用 ArrayList。

1、成员变量

  首先看一下 Vector 的成员变量

protected Object[] elementData; // 和 ArrayList 相同,使用了一个对象数组用来存储元素
protected int elementCount; // 有效元素的数量
protected int capacityIncrement; // 数组的容量自动增加的量,可能大于、等于或小于0

2、构造函数

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); // capacityIncrement 为0
}
public Vector() {
        this(10); // 无参数是,初始容量为10,和 ArrayList 一样
}
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) // 数组类型是Object数组时,将elementData 转化为Object数组
            elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}

3、成员方法

  (1) add(E e) 方法

public synchronized boolean add(E e) {
        modCount++; // 先将 modCount+1
        ensureCapacityHelper(elementCount + 1);  // 类似 ArrayList 的ensureCapacityInternal,内部实现有所不同
        elementData[elementCount++] = e;
        return true;
}
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
            Integer.MAX_VALUE :
            MAX_ARRAY_SIZE;
    }

   已 add(E e) 方法仅作为一个例子,可以看到 Vector 和 ArrayList 的 add 方法实现的方式差不多,不同的是方法前面通过 synchronize 关键字进行修饰,表明对于同一个 Vector 对象,只允许同一条线程对其进行操作。其实,对用 Vector 中所有的 public 访问修饰符的方法(除了spliterator()方法,包含equals()和hashCode()),都直接或者间接地使用了 synchronized 进行修饰,通过这种方式保证线程安全。

  (2)subList(int fromIndex, int toIndex)

public synchronized List<E> subList(int fromIndex, int toIndex) {
        return Collections.synchronizedList(super.subList(fromIndex, toIndex),
                                            this);
}

  看一下 Collections.synchronizedList 的实现

public static <T> List<T> synchronizedList(List<T> list) {
        return (list instanceof RandomAccess ?
                new SynchronizedRandomAccessList<>(list) :
                new SynchronizedList<>(list));
    }

static <T> List<T> synchronizedList(List<T> list, Object mutex) {
        return (list instanceof RandomAccess ?
                new SynchronizedRandomAccessList<>(list, mutex) :
                new SynchronizedList<>(list, mutex));
}

// SynchronizedRandomAccessList 和 SynchronizedList 是 Collections的静态内部类,支持同步,也是通过 synchronized 实现,但是是以另一种形式

  由于 Vector 实现了 RandomAccess 接口,因此返回的是 SynchronizedRandomAccessList。

java容器体系(四)----List(Vector)

原文:https://www.cnblogs.com/guaniu2750/p/13647678.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!