These two classes both 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:
ArraylistIs 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.
VectorLike arraylist, it 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 to avoid inconsistency caused by simultaneous write of multiple threads, however, implementing synchronization requires a high cost. Therefore, accessing it is slower than accessing arraylist.
ShortlistIt uses a linked list structure to store data. It is suitable for dynamic data insertion and deletion, 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. Check the Java source code and find that when the array size 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
Public Boolean add (E ){
Ensurecapacity (size + 1); // Add elements to determine whether they can be accommodated. If not, create an array.
Elementdata [size ++] = E;
Return true;
}
Public void ensurecapacity (INT mincapacity ){
Modcount ++;
Int oldcapacity = elementdata. length;
If (mincapacity> oldcapacity ){
Object olddata [] = elementdata; // This row is not useful, and developers do not know why
Int newcapacity = (oldcapacity * 3)/2 + 1; // increase the size of the new array by 1.5 times + 1
If (newcapacity <mincapacity)
Newcapacity = mincapacity;
// Mincapacity is usually close to size, so this is a win:
Elementdata = arrays. copyof (elementdata, newcapacity );
}
}
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 );
}
}
The differences between arraylist and vector are as follows:
When the memory is insufficient, arraylist expands to 1.5 times + 1 by default, and vector expands to 2 times by default.
Vector provides the indexof (OBJ, start) interface. arraylist does not
Vector is thread-safe, but vector is not used in most cases, because thread security requires greater system overhead.
Original post address: http://www.cnblogs.com/wanlipeng/archive/2010/10/21/1857791.html