The difference between vector and ArrayList in "language" Java

Source: Internet
Author: User

First of all, both classes implement the list interface, 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 the repetition of elements. The related differences between the 3 specific implementation classes are as follows:

    1. ArrayList is the most commonly used list implementation class, implemented internally by an array, which allows for fast random access to elements. The disadvantage of an array is that there can be no interval between each element, and when the array size does not meet the need to increase storage capacity, it is necessary to copy the data of the array into the new storage space. When inserting or deleting elements from the middle of a ArrayList, it is necessary to copy, move, and cost the array. Therefore, it is suitable for random lookups and traversal, and is not suitable for insertions and deletions.
    2. Vectors, like ArrayList, are also implemented through arrays, except that it supports thread synchronization, where only one thread can write vectors at a time, avoiding inconsistencies caused by simultaneous writing of multiple threads, 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 very suitable for the dynamic insertion and deletion of data, and the random access and traversal speed is relatively slow. In addition, he provides methods that are not defined in the list interface, specifically for manipulating the header and footer elements, and can be used as stacks, queues, and bidirectional queues.

Look at the Java source code, and find that when the size of the array is not enough, you need to re-establish the array, and then copy the elements into the new array, the size of the ArrayList and vector extended array is different.

In ArrayList:

1Public BooleanAdd (e e) {
2
3Ensurecapacity (Size+ 1);//Add elements to determine if they can be accommodated. Create a new array if you can't.
4
5Elementdata[size++]=E
6
7 Return True;
8
9}
10
11Public voidEnsurecapacity (Intmincapacity) {
12
13Modcount++;
14
15 IntOldcapacity=Elementdata.length;
16
17 If(mincapacity>oldcapacity) {
18
19Object olddata[]=Elementdata;//This trip did not see the usefulness, do not know what the developers think
20
21st IntNewcapacity=(oldcapacity* 3)/2 + 1;//Increase the size of the new array
22
23 If(newcapacity<mincapacity)
24
25Newcapacity=mincapacity;
26
27 // mincapacity is usually close to size, so the is a WIN:
28  
Span style= "color: #008080;" >29 Elementdata = 30
31 }
32
33 }
34
35  

In Vector:

1Private voidEnsurecapacityhelper (Intmincapacity) {
2
3 IntOldcapacity=Elementdata.length;
4
5 If(mincapacity>oldcapacity) {
6
7Object[] OldData=Elementdata;
8
9 IntNewcapacity=(capacityincrement> 0)?
10
11(oldcapacity+capacityincrement): (oldcapacity* 2);
12
13 If(newcapacity<mincapacity) {
14
15Newcapacity= mincapacity;

17 }
18
19 Elementdata = arrays.copyof (Elementdata, newcapacity );
20
21 }
22
23 }
24
25

The differences between ArrayList and vectors are as follows:

      1. ArrayList default is 50% + 1 when memory is insufficient, vector is 1 time times the default extension.
      2. The vector provides the indexof (obj, start) interface, ArrayList not.
      3. Vectors are thread-safe, but in most cases do not use vectors, because thread safety requires greater overhead.

The difference between vector and ArrayList in "language" Java

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.