zoukankan      html  css  js  c++  java
  • Java中Vector和ArrayList的区别

    首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList、Vector和LinkedList。List用于存放多个元素,能够维护元素的次序,并且允许元素的重复。3个具体实现类的相关区别如下:

    1. ArrayList是最常用的List实现类,内部是通过数组实现的,它允许对元素进行快速随机访问。数组的缺点是每个元素之间不能有间隔,当数组大小不满足时需要增加存储能力,就要讲已经有数组的数据复制到新的存储空间中。当从ArrayList的中间位置插入或者删除元素时,需要对数组进行复制、移动、代价比较高。因此,它适合随机查找和遍历,不适合插入和删除。
    2. Vector与ArrayList一样,也是通过数组实现的,不同的是它支持线程的同步,即某一时刻只有一个线程能够写Vector,避免多线程同时写而引起的不一致性,但实现同步需要很高的花费,因此,访问它比访问ArrayList慢。
    3. LinkedList是用链表结构存储数据的,很适合数据的动态插入和删除,随机访问和遍历速度比较慢。另外,他还提供了List接口中没有定义的方法,专门用于操作表头和表尾元素,可以当作堆栈、队列和双向队列使用。

    查看Java源代码,发现当数组的大小不够的时候,需要重新建立数组,然后将元素拷贝到新的数组内,ArrayList和Vector的扩展数组的大小不同。

    ArrayList中:

    1. public void add(int index, E element) {
    2. if (index > size || index < 0)
    3. throw new IndexOutOfBoundsException(
    4. "Index: "+index+", Size: "+size);
    5. ensureCapacity(size+1); // Increments modCount!!
    6. System.arraycopy(elementData, index, elementData, index + 1,
    7. size - index);
    8. elementData[index] = element;
    9. size++;
    10. }
    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. }

    Vector中:
    1. public synchronized void addElement(E obj) {
    2. modCount++;
    3. ensureCapacityHelper(elementCount + 1);
    4. elementData[elementCount++] = obj;
    5. }
    6. private void ensureCapacityHelper(int minCapacity) {
    7. int oldCapacity = elementData.length;
    8. if (minCapacity > oldCapacity) {
    9. Object[] oldData = elementData;
    10. int newCapacity = (capacityIncrement > 0) ?
    11. (oldCapacity + capacityIncrement) : (oldCapacity * 2);
    12. if (newCapacity < minCapacity) {
    13. newCapacity = minCapacity;
    14. }
    15. elementData = Arrays.copyOf(elementData, newCapacity);
    16. }
    17. }
    关于ArrayList和Vector区别如下:
    1. ArrayList在内存不够时默认是扩展50% + 1个,Vector是默认扩展1倍。
    2. Vector提供indexOf(obj, start)接口,ArrayList没有。
    3. Vector属于线程安全级别的,但是大多数情况下不使用Vector,因为线程安全需要更大的系统开销。





  • 相关阅读:
    解决问题方法论
    mac os x命令行查找文件
    Ubuntu 更改文件夹权限及chmod详细用法
    Unable to mount the CD/DVD image virtualbox解决方法
    macbook air电池保养方法
    安装mac os x时about a second remaining解决方法
    No qualifying bean of type 'org.springframework.scheduling.TaskScheduler' available
    java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver
    java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)解决方案
    使用springboot和easypoi进行的数据导出的小案例
  • 原文地址:https://www.cnblogs.com/sunhan/p/4dd12cc633fc25145403e290bc80d8ed.html
Copyright © 2011-2022 走看看