The difference between vector and ArrayList in Java _java

Source: Internet
Author: User
First of all, the list interface is implemented by both classes, and the list interface has three implementation classes, namely ArrayList, Vector, and LinkedList. The list is used to hold multiple elements, to maintain the order of elements, and to allow for duplication of elements.
The relevant differences between the 3 specific implementation classes are as follows:

1.ArrayList is the most commonly used list implementation class, which is implemented internally through arrays, allowing for fast random access to elements. The disadvantage of an array is that there can be no spacing between each element, and when the array size is not satisfied, the storage capacity needs to be increased, so that the data already in the array is copied into the new storage space. When inserting or deleting elements from the middle of the ArrayList, it is necessary to copy, move, and cost the array. Therefore, it is suitable for random lookup and traversal, unsuitable for inserting and deleting.

2.Vector, like ArrayList, is also implemented through arrays, except that it supports the synchronization of threads, that is, only one thread at a time can write vectors, avoiding inconsistencies caused by multiple threads writing simultaneously, but achieving synchronization requires a high cost, so Accessing it is slower than accessing ArrayList.

3.LinkedList is used to store data in a linked list structure, which is suitable for dynamic insertion and deletion of data, random access and slow traversal speed. In addition, he provides methods that are not defined in the list interface, specifically for manipulating headers and footer elements, and can be used as stacks, queues, and bidirectional queues.

Look at the Java source and find that when the array is not large enough, the array needs to be ArrayList, and then the elements are copied into the new array, and the size of the extended array of vectors is different.
In ArrayList:
Copy Code code as follows:

Public boolean Add (E e) {
Ensurecapacity (size + 1); Increase the element to determine whether it can be accommodated. Create a new array if you can't.
elementdata[size++] = e;
return true;
}
public void ensurecapacity (int mincapacity) {
modcount++;
int oldcapacity = Elementdata.length;
if (Mincapacity > Oldcapacity) {
Object olddata[] = elementdata; This trip did not see the usefulness, do not know what the developers to consider
int newcapacity = (oldcapacity * 3)/2 + 1; Increase the size of the new array
if (Newcapacity < mincapacity)
newcapacity = mincapacity;
Mincapacity is usually close to size, and so this is a win:
Elementdata = arrays.copyof (Elementdata, newcapacity);
}
}

Vector Medium:
Copy Code code as follows:

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);
}
}

The difference between ArrayList and vectors is as follows:
1.ArrayList The default is to expand 50% + 1 when there is not enough memory, and the vector is 1 time times the default extension.
2.Vector provides indexof (obj, start) interface, ArrayList not.
3.Vector is at the thread-safe level, but in most cases vector is not used because thread safety requires a greater system overhead.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.