Java Learning Note 30

Source: Internet
Author: User

The LinkedList class is the implementation class of the list interface, which is a list collection that can randomly access the collection elements based on the index. In addition, LinkedList


Now the Deque interface, so it can be used as a double-ended queue, can also be used as a "stack" to use. LinkedList is a chain-based linear table.


The following is an official description of the LinkedList class:

The list interface is implemented as a link listing. Implements all optional list operations, and allows all elements (including null). In addition to implementing the List interface


, theLinkedList class also provides a uniform naming method for the get,remove , and insert elements at the beginning and end of the list. These actions allow links to be


Lists are used as stacks, queues, or double-ended queues.


This class implements the Deque interface for add,poll , and other stack and double-ended queue operations.


All actions are performed as required by the list of double links. An operation that is indexed in a list traverses the list from the beginning or the end, from near the specified index


End).


Note that this implementation is not synchronous. if more than one thread accesses a list of links at the same time, and at least one of the threads modifies the list from the fabric, the


It must be kept externally synchronized. (A structural modification is any action that adds or removes one or more elements; The value of the set element is not a structural modification.) ) This


This is typically done by synchronizing the objects that are naturally encapsulated in the list. If such an object does not exist, you should make the


with collections.synchronizedlist method to "wrap" the list. It's a good idea to do this at the time of creation to prevent unexpected, unplanned visits to the list


Ask, as shown below:

   List List = Collections.synchronizedlist (new LinkedList (...));


of this class iterator and the Listiterator the iterator that the method returns is Fast Failure : After the iterator is created, if the list is modified from the structure,


except through the iterator's own Remove or Add method, any other modification at any time in any way, the iterator will throw


concurrentmodificationexception . Therefore, in the face of concurrent changes, the iterator will soon fail completely without risking any future uncertainty


Risk of uncertain behavior.


Note that the fast failure behavior of iterators is not guaranteed and, in general, there is no guarantee that any hard guarantees can be made when there are concurrent changes that are out of sync. Fast


The speed failure iterator tries its best to throw concurrentmodificationexception. Therefore, the way to write a program that relies on this exception is wrong,


The correct approach is that the fast failure behavior of iterators should only be used to detect program errors.


The following are the most methods of the LinkedList class:


public class Main {public static void main (string[] args) {LinkedList linkedlist=new linkedlist ();/* * Boolean Add (e e) *  Adds the specified element to the end of this list.   */linkedlist.add ("A");//output: [A]system.out.println (LinkedList);/* * void Add (int index, E Element) * Insert the specified element at the location specified in this list. */linkedlist.add (1, "B");//output: [A, B]system.out.println (LinkedList);/* * Boolean addall (collection<? extends e> C * Add all the elements in the specified collection to the end of this list, * order is the order in which the iterators that specify collection return the elements. */arraylist arraylist=new ArrayList () arraylist.add ("C"); Arraylist.add ("D"); Linkedlist.addall (ArrayList);//output: [A , B, C, D]system.out.println (LinkedList),/* * Boolean addall (int index, COLLECTION&LT;? extends e> C) * will specify collect  All elements in ion insert this list starting at the specified position. */arraylist list=new ArrayList () list.add ("E"); List.add ("F"); Linkedlist.addall (4, list);//output: [A, B, C, D, E, F]  System.out.println (LinkedList);/* void AddFirst (e e) * Inserts the specified element at the beginning of this list. */linkedlist.addfirst ("First");//output: [First, A, B, C, D, E, F]system.out.println (LinkedList);/* voidAddLast (E) * Adds the specified element to the end of this list. */linkedlist.addlast ("last");//output: [First, A, B, C, D, E, F, Last]system.out.println (LinkedList);/* * Object Clone () * return  Returns a shallow copy of this linkedlist. */linkedlist linkedlistclone= (LinkedList) Linkedlist.clone (); Linkedlistclone.add ("clone");//output: [First, A, B, C, D, E, F, Last, Clone]system.out.println (linkedlistclone);//output: [First, A, B, C, D, E, F, Last]system.out.println (LinkedList);   * * Boolean contains (Object o) * Returns True if this list contains the specified element.  *///output: TrueSystem.out.println (Linkedlist.contains ("first"));/* * iterator<e> descendingiterator () *  Returns an iterator that iterates over the elements of this double-ended queue in reverse order. */iterator iterator=linkedlist.descendingiterator ();//output: Last F E D C B A First while (Iterator.hasnext ()) { System.out.print (Iterator.next () + "");}  System.out.println ();/* * E element () * Gets the header (the first element) without removing the list. *///output; FirstSystem.out.println (Linkedlist.element ());//output: [First, A, B, C, D, E, F, Last]system.out.println (LinkedList);/* * E  Get (int index) * Returns the element at the specified position in this list. *///output: Asystem.out.println (Linkedlist.get (1));/* * E getfirst () * Returns the first element of this list.  *///output: FirstSystem.out.println (Linkedlist.getfirst ());/* * E getlast () * Returns the last element of this list. *///output: LastSystem.out.println (Linkedlist.getlast ());/* * int indexOf (Object o) * Returns the index of the specified element in this list for the first time, * If the element is not included in this list, the return  Back-1. *///output: 4system.out.println (Linkedlist.indexof ("D"));/* * int lastIndexOf (Object o) * Returns the index of the specified element that appears last in this list, *  Returns 1 if the element is not included in this list. *///output: 6system.out.println (Linkedlist.lastindexof ("F"));/* * listiterator<e> listiterator (int index) *  Returns the list iterator (in the appropriate order) of the elements in this list, starting at the specified position in the list. */listiterator Listiterator=linkedlist.listiterator (3);//output: C D E F last while (Listiterator.hasnext ()) { System.out.print (Listiterator.next () + "");}  System.out.println ();/* * Boolean offer (e e) * Adds the specified element to the end of this list (the last element). */linkedlist.offer ("offer");//output: [First, A, B, C, D, E, F, Last, Offer]system.out.println (LinkedList),/* * Boolean Offerf  Irst (e E) * Insert the specified element at the beginning of this list. */linkedlist.offerfirst ("Offerfirst");//output: [Offerfirst, FirsT, A, B, C, D, E, F, Last, Offer]system.out.println (linkedlist);/* Boolean offerlast (e) * Insert the specified element at the end of this list. */linkedlist.offerlast ("Offerlast");//output: [Offerfirst, First, A, B, C, D, E, F, last, offer, offerlast]system.out.println  (linkedlist);/* * E peek () * Gets but does not remove the header (the first element) of this list. *///output: OfferFirstSystem.out.println (Linkedlist.peek ());//output: [Offerfirst, First, A, B, C, D, E, F, last, offer, offerlast  ]system.out.println (LinkedList);/* * E peekfirst () * Gets but does not remove the first element of this list; * Returns NULL if this list is empty. *///output: OfferFirstSystem.out.println (Linkedlist.peekfirst ());//output: [Offerfirst, First, A, B, C, D, E, F, last, offer,  Offerlast]system.out.println (LinkedList);/* * E peeklast () * Gets but does not remove the last element of this list, or null if the list is empty. *///output: OfferLastSystem.out.println (Linkedlist.peeklast ());//output: [Offerfirst, First, A, B, C, D, E, F, last, offer, Offerlast]system.out.println (LinkedList);/* * E poll () * Gets and removes the header (first element) of this list *///output: OfferFirstSystem.out.println ( Linkedlist.poll ());//output: [First, A, B, C, D, E, F, LasT, offer, OFFERLAST]SYSTEM.OUT.PRINTLN (LinkedList);/* * E pollfirst () * Gets and removes the first element of this list, or returns null if this list is empty. *///output: FirstSystem.out.println (Linkedlist.pollfirst ());//output: [A, B, C, D, E, F, last, offer, Offerlast]  System.out.println (LinkedList);/* * E polllast () * Gets and removes the last element of this list, or null if the list is empty. *///output: OfferLastSystem.out.println (Linkedlist.polllast ());//output: [A, B, C, D, E, F, Last, Offer]system.out.println (  LinkedList) */* E pop () * POPs an element at the stack represented by this list. *///output: ASystem.out.println (Linkedlist.pop ());//output: [B, C, D, E, F, Last, Offer]system.out.println (linkedlist); */* void  Push (e) * pushes elements into the stack represented by this list. */linkedlist.push ("a");//output: [A, B, C, D, E, F, Last, Offer]system.out.println (linkedlist);/* * E Remove () * Gets and removes the header of this list (  First element). *///output: ASystem.out.println (Linkedlist.remove ());//output: [B, C, D, E, F, Last, Offer]system.out.println (linkedlist); */* E  Remove (int index) * Removes the element at the specified position in this list. *///output: OfferSystem.out.println (Linkedlist.remove (6));//output: [B, C, D, E, F, Last]systeM.out.println (LinkedList);/* * Boolean remove (Object o) * Removes the first occurrence of the specified element (if present) from this list.   */linkedlist.remove ("last");//output: [B, C, D, E, f]system.out.println (LinkedList);/* * e Removefirst () * Remove and return the first element of this list. *///output: BSystem.out.println (Linkedlist.removefirst ());//output: [C, D, E, F]system.out.println (linkedlist); */* Boolean  Removefirstoccurrence (Object o) * Removes the first occurrence of the specified element from this list (when traversing the list from head to tail).  */linkedlist.removefirstoccurrence ("D");//output: [C, E, F]system.out.println (LinkedList);/* * e removelast () *  Removes and returns the last element of this list. *///output: FSystem.out.println (Linkedlist.removelast ());//output: [C, E]system.out.println (LinkedList);/* * Boolean  Removelastoccurrence (Object o) * Removes the last occurrence of the specified element from this list (when traversing the list from head to tail).  */linkedlist.removelastoccurrence ("E");//output: [C]system.out.println (LinkedList);/* * E Set (int index, E Element) *  Replaces the element at the specified position in this list with the specified element. *///output: CSystem.out.println (linkedlist.set (0, "set"));//output: [Set]system.out.println (LinkedList);/* * int size () *  Returns the number of elements in this list. *///output: 1system.out.println (linkedlist.size());/* * object[] ToArray () * Returns an array of all the elements in this list, in the appropriate order (from the first element to the last element).  *///output: [Set]system.out.println (Arrays.tostring (Linkedlist.toarray ()));/* * void Clear () * Removes all elements from this list. */linkedlist.clear ();//output: []system.out.println (LinkedList);}}



Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/44134565 Emotional Control _ 



Java Learning Note 30

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.