zoukankan      html  css  js  c++  java
  • 源码分析三(Vector与ArrayList的区别)

     前面讨论过ArrayList与LinkedList的区别,ArrayList的底层数据结构是数组Object[],而LinkedList底层维护

    的是一个链表Entry,所以对于查询,肯定是ArrayList的效率高,但是对于删除和插入则是LinedList效率高。

    现在我们再来看看Vector与ArrayList的区别,直接上源码,ArrayList源码:

    1:扩容方面,ArrayList扩容扩原来容量的3/2+1,而Vector扩容为原来容量的2倍

     1 public void ensureCapacity(int minCapacity) {
     2     modCount++;
        //容器中元素的数量
    3 int oldCapacity = elementData.length; 4 if (minCapacity > oldCapacity) { 5 Object oldData[] = elementData;
          //新的容量
    6 int newCapacity = (oldCapacity * 3)/2 + 1; 7 if (newCapacity < minCapacity) 8 newCapacity = minCapacity; 9 // minCapacity is usually close to size, so this is a win: 10 elementData = Arrays.copyOf(elementData, newCapacity); 11 } 12 }

    2:Vector源码

     1  private void ensureCapacityHelper(int minCapacity) {
     2     int oldCapacity = elementData.length;
     3     if (minCapacity > oldCapacity) {
     4         Object[] oldData = elementData;
     5         int newCapacity = (capacityIncrement > 0) ?
     6         (oldCapacity + capacityIncrement) : (oldCapacity * 2);
     7             if (newCapacity < minCapacity) {
     8         newCapacity = minCapacity;
     9         }
    10             elementData = Arrays.copyOf(elementData, newCapacity);
    11     }
    12     }

    默认创建的Vector对象new Vector(); 默认

    capacityIncrement = 0

    1  public Vector() {
    2     this(10);
    3     }

    调一个参数的构造方法:

    1  public Vector(int initialCapacity) {
    2     this(initialCapacity, 0);
    3     }

    默认增长容量为0

    调用两个参数的构造方法:

    1   public Vector(int initialCapacity, int capacityIncrement) {
    2     super();
    3         if (initialCapacity < 0)
    4             throw new IllegalArgumentException("Illegal Capacity: "+
    5                                                initialCapacity);
    6     this.elementData = new Object[initialCapacity];
    7     this.capacityIncrement = capacityIncrement;
    8     }

    所以

     int newCapacity = (capacityIncrement > 0) ?
             (oldCapacity + capacityIncrement) : (oldCapacity * 2);

    新的容量扩展到原来的2倍。

    再来看它们的另外一个区别,ArrayList类中的方法都是没有synchronized修饰的,所以都是非线程安全的,

    获取集合中元素数量:

    1  public int size() {
    2     return size;
    3     }

    判断集合是否为空:

    1  public boolean isEmpty() {
    2     return size == 0;
    3     }
    1   public boolean contains(Object o) {
    2     return indexOf(o) >= 0;
    3     }

    。。。。。

    再来看看Vector类,方法都是由synchronized修饰,所以是线程安全的,万事都是有利有弊,线程安全的处理数据效率会低,而线程非安全的处理

    数据效率相对高一些:

     1 public synchronized void setSize(int newSize) {
     2     modCount++;
     3     if (newSize > elementCount) {
     4         ensureCapacityHelper(newSize);
     5     } else {
     6         for (int i = newSize ; i < elementCount ; i++) {
     7         elementData[i] = null;
     8         }
     9     }
    10     elementCount = newSize;
    11     }
    1 public synchronized int capacity() {
    2     return elementData.length;
    3     }
    1   public synchronized int size() {
    2     return elementCount;
    3     }
    1   public synchronized boolean isEmpty() {
    2     return elementCount == 0;
    3     }

    。。。。。。

  • 相关阅读:
    mysqldump 导出数据库为DBname的表名为Tname的表结构 导出数据库的所有表的表结构
    mysqldump 备份某张表 Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions,
    nfs missing codepage or helper program, or other error
    date 增加一个小时 减少一个小时
    mysqldump 备份单个数据库
    mysql删除账户
    怎么删除某个用户的所有帖子?
    mongodb删除重复数据
    ReSharper2018破解详细方法
    激活windows和office
  • 原文地址:https://www.cnblogs.com/warrior4236/p/6055746.html
Copyright © 2011-2022 走看看