2022-03-12——ArrayList和Vector主要区别是什么?

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

LinkedList主要保持数据的插入顺序的时候使用,采用链表结构。

ArrayList,Vector主要区别为以下几点:
(1):Vector是线程安全的,源码中有很多的synchronized可以看出,而ArrayList不是。导致Vector效率无法和ArrayList相比;
(2):ArrayListVector都采用线性连续存储空间,当存储空间不足的时候,ArrayList默认增加为原来的50%,Vector默认增加为原来的一倍;
(3):Vector可以设置capacityIncrement,而ArrayList不可以,从字面理解就是capacity容量,Increment增加,容量增长的参数。

源码分析:
首先看看构造器:
ArrayList:三个

    /**
     * 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:四个

//构造一个指定默认长度的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();
       ...
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值