The analysis of LinkedList will start with and only from its Add method.
Because the previous article has analyzed the ArrayList, the similar place no longer narrates, the attention point in the linkedlist characteristic.
Property:
/** * List head * / transient node<e> first ; /** * The Tail of the chain * / transient node<e> last;
View Code
From the above two attributes, we can conclude that LinkedList is based on a doubly linked list.
Node code (without too much explanation):
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; } }
View Code
LinkedList the Add Method:
There are multiple add methods in the LinkedList, which is the same, then select one to analyze.
/*** Insert the specified element to the list tail*/ Public BooleanAdd (e e) {linklast (e); return true; } /*** Links e as last element. */ voidLinklast (e e) {Finalnode<e> L =Last ; FinalNode<e> NewNode =NewNode<> (L, E,NULL); Last=NewNode; if(L = =NULL) First=NewNode; ElseL.next=NewNode; Size++; Modcount++;//This series of blogs is explained in detail}
JDK Source Reading Notes Java Collection framework (iv) (LinkedList)