LinkedList in-depth learning

Source: Internet
Author: User

    • Implementation method

First LinkedList inherited the abstractsequentiallist realized the List<e>, Deque<e>, cloneable, Java.io.Serializable Interface

LinkedListis a kind of commonListimplementation, he is based on a doubly linked list ( doubly linked list: Each element in the collection knows its previous element and the position of the latter element ), inLinkedList, use an internalEntityclass to represent the elements in the collection, the value of the element is assigned to theelementProperties,Entityin theNextproperty points to the next element of the element,Entityin thepreviousThe property points to the previous element of the element, and is based on a mechanism that enables fast movement of elements in the collection.

    • Construction method

creating linkedlist element null next null previous null entity header

when the constructor is executed, LinkedList will be Head of the Next and previous all point to Header To form the closed loop required for a doubly linked list     

    • Insert object: Add (E)

every time you passAddmethod to add elements, the only thing to do is to create aEntityobject, and put thisEntityof theNextPointingHeader,previousPointinghead.previous, in the completion of their ownNext,Previuosis set, and the next element of the current element ispreviouspointing to itself, and will be located in the previous element of the current elementNextpoint to yourself so that the closed loop of the doubly linked list is maintained.

LinkedList of the Add method is not like ArrayList That way, consider the problem of scaling and copying arrays, but each additional element will create a new Entity object, and modifies the properties of the adjacent two elements

    • Delete object: Remove (E)

to delete LinkedList an element in the same way, first traversing the entire set of elements, traversing and looking for elements of the method and ArrayList basically the same, looking to delete later than ArrayList it's much simpler.

when deleted, the value needs to be deleted directly from the current element on the linked list, and the element , previous , Next set to NULL, does not need to move the position of the element

    • Get a single object: Get (Index)

LinkedListelement is not stored in the array, so hisGetoperation process thanArrayListcomplex, in the executionGetoperation, first determine the incomingIndexvalue is less than0or greater than or equal to the currentLinkedListof thesizevalue, non-conforming throws an out-of-bounds exception, or, if qualified, determines whether the currently acquired position is less thanLinkedListThe general value, if less than, is always found from the beginningIndexthe element that corresponds to the position, which is greater than the trailing forward

    • Traversing objects: iterator ()

Iterator method is by the parent class's absttractlist implementation, when this method is called, each time it creates a Listitr object that is created when the object conforms to the Save cursor location

    • To determine whether an object exists: contains (E)

In order to determine whether an element exists in the collection, The method used is to traverse all elements if the value passed in is null null null equals Judging, finding it returns true

    • Attention points
      • LinkedList is based on a two-way linked list mechanism .
      • LinkedList non-thread-safe
      • LinkedList when you execute an INSERT element, you create a Entity Object, and toggles the reference of the element before and after the element, while looking for the element, traversing the linked list, deleting the element, traversing the linked list to find the deleted element

LinkedList in-depth learning

Related Article

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.