What is the difference between ArrayList and LinkedList?
ArrayList 和 LinkedList
What are the differences between ArrayList and LinkedList before? This is one of the most common questions asked in interviews. Follow the source code analysis to see the difference between the two.
The underlying data structure is different
Entering the source code of ArrayList, you can see that the member variable of ArrayList has an Object array, and this data is used to store the inserted data.
Also enter the source code of LinkedList, you can see that the member variable maintainer has two nodes, one is the first node and the other is the last node. In fact, the bottom layer of LinkedList is a doubly linked list.
The underlying implementation of the operation collection is different
insert operation
It is easy to understand that the underlying data structure is different, and the underlying operations that directly affect the operation set are different. You may need to have a certain understanding of the data structure here.
The first is insertion. According to the knowledge of the data structure we have learned, we know that the elements in the array are stored in a continuous space, but the linked list is not. Therefore, according to the index search situation, the array can immediately find the data only according to the offset. Here we look at it in combination with the source code.
The following is the add method of ArrayList.
First of all, it is necessary to judge whether the index index is legal when inserting. The inserted index can only be [0,size]
, which is also easy to understand, otherwise it is easy to cause the array to go out of bounds.
Then call the arraycopy method of System, which is a navite method, indicating that this method is not implemented by Java. The effect is to copy the array A to the array B, and specify the interval, here is the data after the index to be inserted ( Including the data pointed to by index), all move one bit backward.
For example:数组[1,2,3,4],index = 2,操作后就会变为[1,2,3,3,4]。
The next step is to insert the element at the position of index, and then size++.
The following is the add method of LinkedList
First of all, it is first to judge whether the insertion position is at the end. If it is the end, just insert it directly. Because it is a double-linked list, we naturally maintain the reference of the last node.
If you encounter the situation of inserting in the middle, which is the most common situation, you need to get the reference of the inserted node first, and then insert it. It is also very interesting to get the reference of the inserted node, because we know that we have both the head pointer and the tail pointer, so we only need to judge which index the inserted index is close to, and then start traversing from this endpoint. The effect here size>>1
is equivalent to size/2
.
delete operation
For the deletion of ArrayList, the target element is found first, and then all the following elements are moved forward.
For the deletion of LinkedList, first traverse to the node to be deleted, and then directly call the unlink method to disconnect the node.
Use the unlink method to disconnect the node to be deleted.
search by index
For ArrayList, it is nothing more than to check whether the parameters are legal, and then return the corresponding elements of the array.
For LinkedList, in addition to judging whether the parameter is legal, it is necessary to find the node of the index (by traversing) before returning the value corresponding to the node.
Summary: In fact, the biggest difference between ArrayList and LinkedList is the difference in the underlying data structure, resulting in different time efficiency when performing the same operation. For example, if you need to frequently search for elements according to the index, you are suitable to use ArrayList; if you need to delete frequently Or add elements, it is suitable to use LinkedList, because ArrayList needs to move a lot of elements, of course, LinkedList needs a traversal to locate before deleting elements, so this is not only absolute. As for the choice of storage space, in fact, the space overhead of ArrayList is mainly on the array. Of course, due to the expansion mechanism, the array will have a certain reserved space; while the space overhead of LinkedList is mainly on the predecessor node and the successor node. , so the space consumption of each element is more than ArrayList.
About thread safety
Both ArrayList and LinkedList are not guaranteed to be thread safe. There is no lock when performing various operations. When the iterator gets the next element, it will check whether it has been modified, and if it has been modified, ConcurrentModificationException
an exception will be thrown.