First, we can see that both of these classes implement the list interface, and the list interface has three implementation classes: arraylist, vector, and sorted list. List is used to store multiple elements, maintain the order of elements, and allow repeated elements. The differences between the three implementation classes are as follows:
- Arraylist is the most common list implementation class, which is implemented through arrays internally. It allows fast Random Access to elements. The disadvantage of an array is that there cannot be an interval between each element. When the array size does not meet the requirements, you need to increase the storage capability. It is necessary to copy the data of an array to a new bucket. When an element is inserted or deleted from the middle of the arraylist, the array needs to be copied, moved, and the cost is relatively high. Therefore, it is suitable for random search and traversal, and is not suitable for insertion and deletion.
- Like arraylist, vector is implemented through arrays. The difference is that it supports thread synchronization, that is, only one thread can write a vector at a time point, avoid inconsistency caused by simultaneous write of multiple threads, but it is costly to implement synchronization. Therefore, it is slower to access arraylist than to access arraylist.
- The shortlist uses a linked list structure to store data. It is suitable for dynamic insertion and deletion of data, and the random access and traversal speed is relatively slow. In addition, it provides methods not defined in the list interface, which are used to operate the header and table tail elements and can be used as stacks, queues, and bidirectional queues.
View JavaSource codeWhen the size of the array is not enough, you need to re-create the array and copy the elements to the new array. The size of the extended array of arraylist and vector is different.
In arraylist:
1 Public Boolean Add (E ){
2
3 Ensurecapacity (size + 1 ); // Add elements to determine whether they can be accommodated. If not, create an array.
4
5 Elementdata [size ++ ] = E;
6
7 Return True ;
8
9 }
10
11 Public Void Ensurecapacity ( Int Mincapacity ){
12
13 Modcount ++ ;
14
15 Int Oldcapacity = Elementdata. length;
16
17 If (Mincapacity > Oldcapacity ){
18
19 Object olddata [] = Elementdata; // I don't know why this trip is useful. I don't know what the developer thinks.
20
21 Int Newcapacity = (Oldcapacity * 3 ) / 2 + 1 ; // Increase the size of the new array
22
23 If (Newcapacity < Mincapacity)
24
25 Newcapacity = Mincapacity;
26
27 // Mincapacity is usually close to size, so this is a win:
28
29 Elementdata = Arrays. copyof (elementdata, newcapacity );
30
31 }
32
33 }
34
35
Vector:
1 Private Void Ensurecapacityhelper ( Int Mincapacity ){
2
3 Int Oldcapacity = Elementdata. length;
4
5 If (Mincapacity > Oldcapacity ){
6
7 Object [] olddata = Elementdata;
8
9 Int Newcapacity = (Capacityincrement > 0 ) ?
10
11 (Oldcapacity + Capacityincrement): (oldcapacity * 2 );
12
13 If (Newcapacity < Mincapacity ){
14
15 Newcapacity = Mincapacity;
16
17 }
18
19 Elementdata = Arrays. copyof (elementdata, newcapacity );
20
21 }
22
23 }
24
25
The differences between arraylist and vector are as follows:
- When the memory is insufficient, arraylist expands by 50% + by default, and vector expands by 1 times by default.
- Vector provides the indexof (OBJ, start) interface, which is not available in arraylist.
- Vector is thread-safe, but vector is not used in most cases, because thread security requires greater system overhead.