As can be seen from the diagram, ArrayList and LinkedList are the list interface implementation class, so all of the implementation of the list of the implementation of the method, but the way of implementation is different, (from which we can see the benefits of interface-oriented, for different requirements have different implementation!), The list interface inherits the collection interface, and the collection interface inherits the Iterable interface, so you can see that the list has both the collection and the Iterable interface features. ArrayList
ArrayList is the implementation of a variable array of the list interface. All optional list operations are implemented and all elements, including NULL, are allowed. In addition to implementing the list interface, this class provides methods to manipulate the size of an array that is used internally to store lists.
Each ArrayList instance has a capacity, which is the size of the array used to store the list elements. It is always at least equal to the size of the list. As you add elements to the ArrayList, the capacity increases automatically. Automatic growth will result in a new copy of the data to the array, so if you can predict the amount of data, you can specify its capacity when constructing ArrayList. Before adding a large number of elements, the application can also use the Ensurecapacity action to increase the capacity of the ArrayList instance, which reduces the number of incremental redistribution.
Note that this implementation is not synchronized. If more than one thread accesses a ArrayList instance at the same time, and at least one of the threads modifies the list from the structure, it must maintain an external synchronization. This is usually done by synchronizing the objects that are used to encapsulate the list. However, if no such object exists, the list needs to be "packaged" using {@link collections#synchronizedlist collections.synchronizedlist}, preferably when the list object is created. In order to avoid the unexpected asynchronous operation of the list.
List List = Collections.synchronizedlist (new ArrayList (...));
It is recommended that you use ArrayList in a single thread, and you can select vectors or copyonwritearraylist in multiple threads. Expansion
An obvious feature of an array is that its capacity is fixed and the capacity cannot be changed once the array is created. So before adding a specified element to an array, the first thing to consider is whether the capacity is saturated.
If the elements in the array exceed their capacity when the next addition occurs, you must enlarge them. Constrained by the invariant nature of array capacity, the essence of dilatancy is to create a new array of larger capacity, and then copy the elements of the old array to the new array.
Here take the ArrayList add operation as an example, to see the process of ArrayList internal array expansion.
Public boolean Add (E e) { //key-> before adding, verify capacity
ensurecapacityinternal (size + 1);
Modifies the size and adds the specified element at the end of the array
elementdata[size++] = e; return true;
}
You can find that ArrayList checks the capacity of the internal array and selectively expands the array before adding the operation. In ArrayList, the ensurecapacityinternal of the array is done by the private method. The following is a detailed implementation process:
The first step in the expansion operation is to determine whether the current ArrayList internal array is empty, and the minimum capacity mincapacity is set to 10.
Default capacity of internal arrays private static final int default_capacity = 10;//Empty internal array private static final object[] Empty_elementdata = {};/ /key-> mincapacity = seize+1, which means that after the add operation, the number of elements in the array is private void ensurecapacityinternal (int mincapacity) { //judge the number of internal Whether the group is an empty
if (elementdata = = empty_elementdata) { //Set the minimum capacity of the array (>=10)
mincapacity = Math.max (default_capacit Y, mincapacity);
}
ensureexplicitcapacity (mincapacity);
}
Then determine if the add operation will result in the capacity saturation of the internal array.
private void ensureexplicitcapacity (int mincapacity) {
modcount++;
The result is true, which means that the next addition will cause the number of elements to exceed the array capacity
if (Mincapacity-elementdata.length > 0) { //True expansion operation
Grow ( mincapacity);
}
With insufficient array capacity, there are two key locations: expansion formulas, expansion operations by copying elements from the old array to the new array.
private static final int max_array_size = integer.max_value-8;
private void Grow (int mincapacity) { int oldcapacity = elementdata.length; //key-> Capacity expansion formula int newcapacity = oldcapacity + (oldCapacity >>
1); //a series of judgements for new capacity if (Newcapacity-mincapacity < 0) {newCapacity = MinC
apacity; &NBSP;&NBSP;&NBSP;&NBSP} if (Newcapacity-max_array_size > 0) {
newcapacity = hugecapacity (mincapacity); &NBSP;&NBSP;&NBSP;&NBSP} //key-> copy old array elements to the new array elementdata =
Arrays.copyof (Elementdata, newcapacity); } private static int hugecapacity (int minCapacity) { if (Mincapacity < 0) { throw new OutOfMemoryError (); &nbsP;&NBSP;&NBSP;&NBSP} return (Mincapacity > Max_array_size)?
Integer.MAX_VALUE:MAX_ARRAY_SIZE; }
For the ArrayList expansion operation, the whole process is as follows:
LinkedList
LinkedList is a doubly linked list that inherits from the Abstractsequentiallist. It can also be manipulated as a stack, a queue, or a two-terminal queue.
LinkedList implements the List interface and can queue operations on it.
LinkedList implements the Deque interface, that is, LinkedList can be used as a two-terminal queue.
LinkedList implements the Cloneable interface, which covers the function clone () and can be cloned.
LinkedList implements the Java.io.Serializable interface, which means that LinkedList supports serialization and can be transmitted through serialization.
LinkedList is not synchronized.
As shown in the figure above, the bidirectional linked list structure used at the bottom of the LinkedList has a head node and a tail node, and a two-way list means that we can traverse from the beginning, or we can begin the reverse traversal from the end, and do the corresponding actions for the head and tail.
private Static Class Node<e> {
E item;
Node<e> Next;
Node<e> prev;
Node (node<e> prev, E element, node<e> next) {
This.item = element;
This.next = Next;
This.prev = prev;
}
Summary
1.ArrayList is a data structure based on dynamic array, LinkedList based on the data structure of the linked list.
2. For random access get and set,arraylist feel better than LinkedList because linkedlist to move the pointer.
3. Add and Remove,linedlist are more advantageous for new and delete operations because ArrayList want to move the data.