Home > Java > javaTutorial > body text

The difference between Vector and ArrayList in Java

高洛峰
Release: 2016-11-16 13:13:31
Original
1278 people have browsed it

First of all, see that both categories implement the List interface, and the List interface has three implementation classes, namely ArrayList, Vector and LinkedList. List is used to store multiple elements, can maintain the order of elements, and allows duplication of elements. The relevant differences between the three specific implementation classes are as follows:

ArrayList is the most commonly used List implementation class. It is implemented internally through an array, which allows fast random access to elements. The disadvantage of the array is that there cannot be a gap between each element. When the array size is not satisfied, the storage capacity needs to be increased, and the data of the existing array must be copied to the new storage space. When inserting or deleting elements from the middle of ArrayList, the array needs to be copied and moved, which is relatively expensive. Therefore, it is suitable for random search and traversal, but not for insertion and deletion.

Vector, like ArrayList, is also implemented through arrays. The difference is that it supports thread synchronization, that is, only one thread can write to Vector at a time, avoiding inconsistency caused by multiple threads writing at the same time. However, achieving synchronization requires a lot of effort. cost, therefore, accessing it is slower than accessing the ArrayList.

LinkedList uses a linked list structure to store data, which is very suitable for dynamic insertion and deletion of data. Random access and traversal are relatively slow. In addition, it also provides methods not defined in the List interface, which are specifically used to operate the head and tail elements of the list, and can be used as a stack, queue, and bidirectional queue.

​ Looking at the Java source code, we found that when the size of the array is not enough, we need to re-create the array and then copy the elements to the new array. The sizes of the extended arrays of ArrayList and Vector are different.

In ArrayList:

public boolean add(E e) {
 
    ensureCapacity(size + 1);  // 增加元素,判断是否能够容纳。不能的话就要新建数组
  
    elementData[size++] = e;
 
    return true;
 
}
 
 public void ensureCapacity(int minCapacity) {
 
    modCount++; 
 
    int oldCapacity = elementData.length;
 
    if (minCapacity > oldCapacity) {
 
        Object oldData[] = elementData; // 此行没看出来用处,不知道开发者出于什么考虑
  
        int newCapacity = (oldCapacity * 3)/2 + 1; // 增加新的数组的大小
  
        if (newCapacity < minCapacity)
 
       newCapacity = minCapacity;
 
            // minCapacity is usually close to size, so this is a win:
  
            elementData = Arrays.copyOf(elementData, newCapacity);
 
    }
 
}
Copy after login

In Vector:

private void ensureCapacityHelper(int minCapacity) {
 
    int oldCapacity = elementData.length;
 
    if (minCapacity > oldCapacity) {
 
        Object[] oldData = elementData;
 
        int newCapacity = (capacityIncrement > 0) ?
 
       (oldCapacity + capacityIncrement) : (oldCapacity * 2);
 
        if (newCapacity < minCapacity) {
 
       newCapacity = minCapacity;
 
        }
 
         elementData = Arrays.copyOf(elementData, newCapacity);
 
    }
 
}
Copy after login

The difference between ArrayList and Vector is as follows:

ArrayList expands by 50% + 1 by default when memory is insufficient, and Vector expands by 1 times by default.

Vector provides the indexOf(obj, start) interface, but ArrayList does not.

Vector is thread-safe, but Vector is not used in most cases because thread safety requires greater system overhead.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template