Java Study Notes 30, java Study Notes

Source: Internet
Author: User
Tags addall concurrentmodificationexception

Java Study Notes 30, java Study Notes

The sortedlist class is the implementation class of the List interface. It is a List set and allows random access to the collection elements based on the index. In addition, the consumer list is also real


The Deque interface can be used as a dual-end queue or a "stack. A chain-based linear table.


The following is an official description of the sort list class:

ListThe implementation of the interface link list. Implement all optional list operations and allow all elements (includingNull). Besides implementationListInterface


,ShortlistClass is also at the beginning and end of the ListGet,RemoveAndInsertElements provide a uniform naming method. These operations allow linking


The list is used as a stack, queue, or double-end queue.


Such implementationDequeInterface, which isAdd,PollProvides first-in-first-out queue operations, as well as other stack and double-end queue operations.


All operations are performed according to the dual-link list. The index editing operation in the list will traverse the list from the beginning or end (from near the specified index


).


Note that this implementation is not synchronous.If multiple threads access a link list at the same time, and at least one thread modifies the list from the structure


ItRequiredMaintain external synchronization. (Structure Modification refers to any operation to add or delete one or more elements. Setting only the element value is not a structure modification .) This


It is generally done by synchronizing the objects that encapsulate the list. If such an object does not exist


Use the Collections. synchronizedList method to "Wrap" the list. It is best to complete this operation at creation to prevent unexpected non-synchronous access to the list.


Q:

   List list = Collections.synchronizedList(new LinkedList(...));


SuchIteratorAndListIteratorThe iterator returned by the method isQuick failureAfter the iterator is created, if the list is modified from the structure,


Unless you use the iterator's ownRemoveOrAddMethod. The iterator throws any modification at any time.


ConcurrentModificationException. Therefore, in the face of concurrent modifications, the iterator will soon fail completely without any uncertain future time.


Risks of uncertain behaviors.


Note: The Fast failure behavior of the iterator cannot be guaranteed. In general, it is impossible to make any hard guarantee when there are non-synchronous concurrent modifications. Fast


Rate failure iterator throws as much as possibleConcurrentModificationException. Therefore, writing programs dependent on this exception is incorrect,


The correct method is:The fast failure behavior of the iterator should only be used to detect program errors.


The following are the vast majority of methods of the sort list class:


Public class Main {public static void main (String [] args) {empty list = new empty list ();/** boolean add (E e) * Add the specified element to the end of the list. */Lists List. add ("A"); // output: [A] System. out. println (random list);/** void add (int index, E element) * Insert the specified element at the specified position in this list. */Sort list. add (1, "B"); // output: [A, B] System. out. println (sort list);/** boolean addAll (Collection <? Extends E> c) * add all elements in the specified collection to the end of this list. * The Order is the order in which the iterator of the specified collection returns these elements. */ArrayList arrayList = new ArrayList (); arrayList. add ("C"); arrayList. add ("D"); returns list. addAll (arrayList); // output: [A, B, C, D] System. out. println (sequence list);/** boolean addAll (int index, Collection <? Extends E> c) * inserts all elements in the specified collection into this list starting from the specified position. */ArrayList list = new ArrayList (); list. add ("E"); list. add ("F"); returns list. addAll (4, list); // output: [A, B, C, D, E, F] System. out. println (sequence list);/** void addFirst (E) * inserts the specified element at the beginning of the list. */Lists List. addFirst ("First"); // output: [First, A, B, C, D, E, F] System. out. println (optional list);/** void addLast (E) * Add the specified element to the end of the list. */Lists List. addLast ("Last"); // output: [First, A, B, C, D, E, F, Last] System. out. println (writable list);/** Object clone () * returns a superficial copy of the writable list. */Shortlist upload listclone = (shortlist) shortlist. clone (); Listen listclone. add ("clone"); // output: [First, A, B, C, D, E, F, Last, clone] System. out. println (repeated listclone); // output: [First, A, B, C, D, E, F, Last] System. out. println (optional list);/** boolean contains (Object o) * returns true if the list contains the specified element. * /// Output: trueSystem. out. println (listing list. contains ("First");/** Iterator <E> descendingIterator () * returns the Iterator that iterates on the elements of the Two-end queue in reverse order. */Iterator iterator = character list. descendingIterator (); // output: Last f e d c B A First while (iterator. hasNext () {System. out. print (iterator. next () + "");} System. out. println ();/** E element () * Get but do not remove the header (the first element) of this list ). * /// Output; FirstSystem. out. println (listing list. element (); // output: [First, A, B, C, D, E, F, Last] System. out. println (random list);/** E get (int index) * returns the elements at the specified position in this list. * // Output: ASystem. out. println (listing list. get (1);/** E getFirst () * returns the first element of this list. * // Output: FirstSystem. out. println (listing list. getFirst ();/** E getLast () * returns the last element of the list. * /// Output: LastSystem. out. println (listing list. getLast ();/** int indexOf (Object o) * returns the index of the specified element that appears for the first time in this list. * If this element is not included in this list, -1 is returned. * /// Output: 4System. out. println (listing list. indexOf ("D");/** int lastIndexOf (Object o) * returns the index of the specified element that appears last in this list. * If this element is not included in this list, -1 is returned. * /// Output: 6System. out. println (listing list. lastIndexOf ("F");/** ListIterator <E> listIterator (int index) * returns the list iterator of the elements in this list (in the appropriate order ), * starts from the specified position in the list. */ListIterator listIterator = ListIterator list. listIterator (3); // output: c d e f Last while (listIterator. hasNext () {System. out. print (listIterator. next () + "");} System. out. println ();/** boolean offer (E e) * Add the specified element to the end of the list (the last element ). */Lists List. offer ("offer"); // output: [First, A, B, C, D, E, F, Last, offer] System. out. println (sequence list);/** boolean offerFirst (E) * Insert the specified element at the beginning of the list. */Lists List. offerFirst ("offerFirst"); // output: [offerFirst, First, A, B, C, D, E, F, Last, offer] System. out. println (optional list);/** boolean offerLast (E) * Insert the specified element at the end of this list. */Lists List. offerLast ("offerLast"); // output: [offerFirst, First, A, B, C, D, E, F, Last, offer, offerLast] System. out. println (optional list);/** E peek () * Get but do not remove the header of this list (the first element ). * // Output: offerFirstSystem. out. println (listing list. peek (); // output: [offerFirst, First, A, B, C, D, E, F, Last, offer, offerLast] System. out. println (sequence list);/** E peekFirst () * Get but do not remove the first element of this list; * If this list is empty, return null. * // Output: offerFirstSystem. out. println (listing list. peekFirst (); // output: [offerFirst, First, A, B, C, D, E, F, Last, offer, offerLast] System. out. println (distinct list);/** E peekLast () * Get but do not remove the last element of this list; if this list is empty, * returns null. * // Output: offerLastSystem. out. println (listing list. peekLast (); // output: [offerFirst, First, A, B, C, D, E, F, Last, offer, offerLast] System. out. println (sequence list);/** E poll () * Get and remove the header (first element) of this list * // output: offerFirstSystem. out. println (listing list. poll (); // output: [First, A, B, C, D, E, F, Last, offer, offerLast] System. out. println (random list);/** E pollFirst () * Get and remove the first element of this list; if this list is empty, * returns null. * /// Output: FirstSystem. out. println (listing list. pollFirst (); // output: [A, B, C, D, E, F, Last, offer, offerLast] System. out. println (optional list);/** E pollLast () * Get and remove the last element of this list; if this list is empty, * returns null. * // Output: offerLastSystem. out. println (listing list. pollLast (); // output: [A, B, C, D, E, F, Last, offer] System. out. println (writable list);/** E pop () * an element pops up from the stack indicated by this list. * /// Output: ASystem. out. println (listing list. pop (); // output: [B, C, D, E, F, Last, offer] System. out. println (publish list);/** void push (E) * pushes the element into the stack represented by this list. */Lists List. push ("A"); // output: [A, B, C, D, E, F, Last, offer] System. out. println (optional list);/** E remove () * Get and remove the header (the first element) of this list ). * /// Output: ASystem. out. println (listing list. remove (); // output: [B, C, D, E, F, Last, offer] System. out. println (random list);/** E remove (int index) * removes the elements at the specified position in this list. * /// Output: offerSystem. out. println (listing list. remove (6); // output: [B, C, D, E, F, Last] System. out. println (optional list);/** boolean remove (Object o) * remove the first specified element from the List (if any ). */Lists List. remove ("Last"); // output: [B, C, D, E, F] System. out. println (revoke list);/** E removeFirst () * remove and return the first element in this list. * /// Output: BSystem. out. println (listing list. removeFirst (); // output: [C, D, E, F] System. out. println (sequence list);/** boolean removeFirstOccurrence (Object o) * removes the specified element that appears for the first time from the List (when traversing the list from the header to the end ). */Lists List. removeFirstOccurrence ("D"); // output: [C, E, F] System. out. println (revoke list);/** E removeLast () * remove and return the last element of this list. * /// Output: FSystem. out. println (listing list. removeLast (); // output: [C, E] System. out. println (sequence list);/** boolean removeLastOccurrence (Object o) * removes the last specified element from the List (when traversing the list from the header to the end ). */Lists List. removeLastOccurrence ("E"); // output: [C] System. out. println (random list);/** E set (int index, E element) * replace the element at the specified position in this list with the specified element. * /// Output: CSystem. out. println (listing list. set (0, "set"); // output: [set] System. out. println (struct list);/** int size () * returns the number of elements in this list. * // Output: 1System. out. println (listing list. size ();/** Object [] toArray () * returns an array containing all elements in the list in the appropriate order (from the first element to the last element. * /// Output: [set] System. out. println (Arrays. toString (listing list. toArray ();/** void clear () * remove all elements from the list. */Shortlist. clear (); // output: [] System. out. println (shortlist );}}



Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/44134565 sentiment control _



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.