ArrayList和Vector的区别

参考:https://blog.csdn.net/ldxlz224/article/details/52574821

List接口下一共实现了三个类:ArrayList,Vector和LinkedList

LinkedList主要保持数据的插入顺序的时候使用,采用链表结构;ArrayList,Vector都是使用的是长度可变的数组存储

一、ArrayList,Vector主要区别为以下几点:

(1)同步性:Vector是线程安全的,用synchronized实现线程安全,而ArrayList是线程不安全的,如果只有一个线程会访问到集合,那最好使用ArrayList,因为它不考虑线程安全,效率会高些;如果有多个线程会访问到集合,那最好是使用Vector,因为不需要我们再去考虑和编写线程安全的代码。

(2)数据容量增长:二者都有一个初始容量大小,采用线性连续存储空间,当存储的元素的个数超过了容量时,就需要增加二者的存储空间,Vector增长原来的一倍,ArrayList增加原来的0.5倍。

二、源码分析:

(1)构造器

ArrayList :三个,默认长度为10

 /**
     * Constructs an empty list with an initial capacity of ten.
     * 构造一个默认初始容量为10的list 
     */
    public ArrayList() {
        super();
        this.elementData = EMPTY_ELEMENTDATA;
    }

    /**
     * 构造一个指定默认长度的list initialCapacity 不能小于0;
     * Constructs an empty list with the specified initial capacity.
     *
     * @param  initialCapacity  the initial capacity of the list
     * @throws IllegalArgumentException if the specified initial capacity
     *         is negative
     */
    public ArrayList(int initialCapacity) {
        super();
        if (initialCapacity < 0)
            throw new IllegalArgumentException("Illegal Capacity: "+
                                               initialCapacity);
        this.elementData = new Object[initialCapacity];
    }




 /**  构造一个包含collection 元素的list
     * Constructs a list containing the elements of the specified
     * collection, in the order they are returned by the collection's
     * iterator.
     *
     * @param c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public ArrayList(Collection<? extends E> c) {
      ...
    }

Vector:4个,默认长度为10

//构造一个指定默认长度的list
  public Vector(int initialCapacity) {
        this(initialCapacity, 0);
    }
 //构造一个默认初始容量为10的list 
  public Vector() {
        this(10);
    }
  //构造一个包含collection 元素的list
  public Vector(Collection<? extends E> c) {
        ...
    }
//区别在于可以设置capacityIncrement
 public Vector(int initialCapacity, int capacityIncrement) {
        super();
       ...
    }

Vector多了一个public Vector(int initialCapacity, int capacityIncrement)构造器,可以设置容量增长,ArrayList是没有的。

(2)主要添加源码分析:

ArrayList类:

 public boolean add(E e) {  
        ensureCapacityInternal(size + 1);  // Increments modCount!!  
        elementData[size++] = e;  
        return true;  
    }  

    private void ensureCapacityInternal(int minCapacity) {  
        modCount++;  
        // 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 + (oldCapacity >> 1); //扩充的空间增加原来的50%(即是原来的1.5倍)  
        if (newCapacity - minCapacity < 0) //如果容器扩容之后还是不够,那么干脆直接将minCapacity设为容器的大小  
            newCapacity = minCapacity;  
        if (newCapacity - MAX_ARRAY_SIZE > 0) //如果扩充的容器太大了的话,那么就执行hugeCapacity  
            newCapacity = hugeCapacity(minCapacity);  
        // minCapacity is usually close to size, so this is a win:  
        elementData = Arrays.copyOf(elementData, newCapacity);  
    }  

Vector类:

 public synchronized boolean add(E e) {  
         modCount++;  
         ensureCapacityHelper(elementCount + 1);  
         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);  

         /** 

         这个扩容需要做个判断:如果容量增量初始化的不是0,即使用的public Vector(int initialCapacity,int capacityIncrement) 

         构造方法进行的初始化,那么扩容的容量是(oldCapacity+capacityIncrement),就是原来的容量加上容量增量的值; 

         如果没有设置容量增量,那么扩容后的容量就是(oldCapacity+oldCapacity),就是原来容量的二倍。 

         **/  

         if (newCapacity - minCapacity < 0)  
             newCapacity = minCapacity;  
         if (newCapacity - MAX_ARRAY_SIZE > 0)  
             newCapacity = hugeCapacity(minCapacity);  
         elementData = Arrays.copyOf(elementData, newCapacity);  
     }  


  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值