Java Collection Class linklist of learning Notes

Source: Internet
Author: User
Tags prev

1. Brief introduction

The bottom of the linklist is actually a doubly linked list, the so-called list is a linklist internal static static class (node), all operations on linklist are essentially through the new Node object in Linklist

Making an association reference

2, the implementation of a, construction method:

Linklist provides a total of two construction methods:

    

 /*** Constructs an empty list. */     PublicLinkedList () {}/*** Constructs a list containing the elements of the specified * collection, in the order they is returned by     The collection ' s * iterator. *     * @paramc The collection whose elements is to being placed into this list *@throwsNullPointerException If the specified collection is null*/     PublicLinkedList (collection<?extendsE>c) { This();    AddAll (c); }

B. Define internal private properties

        

transient int size = 0;     /**      * Pointer to first node.     * Invariant: (first = = NULL && last = = null) | |     *            (First.prev = = NULL && first.item! =     null) */    transient node<e > First ;     /** * Pointer to the last      node.     * Invariant: (first = = NULL && last = = null) | |     *            (Last.next = = NULL && last.item! =     null) */    transient node<e > Last;

Linklist has only three internal properties, one is the length of the linklist, the other two are the first node and the last node, and each time the update operation is performed on the linklist, the values of the first node and the last node change accordingly.

3, linklist Operation increased operation: Add (E E):
/*** Appends the specified element to the end of this list. * * <p>this method is equivalent to {@link#addLast}. *     * @parame element to is appended to the this list *@return {@codetrue} (as specified by {@linkCollection#add}) */     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++; }

Call the Add (e) Operation linklist a Node object and set the previous element of the node to the last node before it, and then point to the new node for the next property of the last node

Add (int index, E Element):
 /*** Inserts the specified element at the specified position in this list.  * Shifts the element currently at, if any, and any * subsequent elements to the right (adds one to their     indices). *     * @paramindex index at which the specified element is inserted *@paramelement element to be inserted *@throwsindexoutofboundsexception {@inheritDoc}     */     Public voidAddintindex, E Element)        {Checkpositionindex (index); if(Index = =size) linklast (element); ElseLinkbefore (element, node (index)); } /*** Inserts element e before Non-null Node succ. */    voidLinkbefore (e E, node<e>succ) {        //assert succ! = null;        Finalnode<e> pred =Succ.prev; FinalNode<e> NewNode =NewNode<>(Pred, E, succ); Succ.prev=NewNode; if(Pred = =NULL) First=NewNode; ElsePred.next=NewNode; Size++; Modcount++; }

Calling Add (int index, e element) is the same as the Add (e Element) method, by changing the value of the next and Prev properties in the index position node, and then pointing the newly created node to the node at the original index position.

 The essence of the delete update operation of linklist is to change the value of the next and prev of the internal node, so there is no longer a description of those operations one by one.

Summarize:

Before because just read arraylist,arraylist inside is through the array to the internal data storage, so began to struggle with this linklist data is stored in, in fact, linklist data is stored directly in the JVM, Because adding data to the linklist is actually a new object of a ArrayList static inner class, except that the first and last properties of Linklist point to references to these objects, so these objects are never recycled by GC. When Linklist deletes an element, it simply deletes the other node's reference to that node, and then the deleted object is reclaimed by GC without reference. This implementation means that the linklist inside the JVM of the elements of the distribution is messy, so when you based on the subscript to get the linklist element is only through one by one to traverse, which also led to its low read efficiency

  

Java Collection Class linklist of learning Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.