Java List Usage Code Analysis very detailed

Source: Internet
Author: User
Tags addall

The principle of a variable group in Java is to constantly create new arrays, adding the original array to the new array, and following the Java list usage in detail.

    • List: The elements are ordered (how to take out, the order will not be chaotic), the elements can be repeated (the angle of 1 on a 3, the angle of 2 can also have a 3) because the collection system has an index
    • ArrayList: The underlying data structure uses the array structure (the array length is variable 50% extension) (characterized by a fast query, but the deletion of the slow) thread is not synchronized
    • LinkedList: The underlying data structure is a linked list structure (characterized by slow query, deletion faster)
    • Vector: The underlying is the array data structure thread synchronization (array length is variable 100% extension) (whether the query or additions and deletions are slow, replaced by ArrayList)

List: The unique method, which can operate the angle of the method is the system-specific method

Boolean Add (int index, E Element) Boolean AddAll (index,collection) 
 Public Static voidList_add () {ArrayList A1=NewArrayList (); A1.add ("Java"); A1.add ("PHP");//the elements in the list collection can be repeatedA1.add (". NET"); System.out.println ("Original collection:" +A1); A1.add (1, "Flash"); A1.add (0, "PS");    SYSTEM.OUT.PRINTLN (A1); ArrayList A2=NewArrayList (); A2.add ("JavaScript"); A2.add ("3dMax"); A2.add ("IBM"); A1.addall (0, a2); SYSTEM.OUT.PRINTLN (A1);}

Deletes the element at the specified position

Boolean remove (int index)
 Public Static void List_remove () {     new  ArrayList ();     A1.add ("JavaScript");     A1.add ("PHP");     A1.add ("flash");     System.out.println ("original set:" +A1);     A1.remove (0);     SYSTEM.OUT.PRINTLN (A1);}

The element set (int index, E Element) that modifies the specified corner mark returns the modified element.

 Public Static void List_set () {     new  ArrayList ();     A1.add ("JavaScript");     A1.add ("PHP");     A1.add (". NET");     System.out.println ("original set:" +A1);     A1.set (1, "Falsh");     SYSTEM.OUT.PRINTLN (A1);}

Check

Get (int index)   returns the element sublist (int fromIndex, int toindex) of the specified position in the list    and returns the portion of the element between FromIndex (including) and Toindex (not included) specified in the list.
 Public Static void List_get () {     new  ArrayList ();     A1.add ("Java");     A1.add ("PHP");     A1.add ("flash");     System.out.println (A1.get (0)); // Gets the element that specifies the corner label, which allows you to iterate through all the elements in the collection      System.out.println (a1.sublist (1, 3)); // gets the element of a part of the collection that contains a header that does not contain a tail }

List collection-specific iterator: Listiterator (is a sub-interface of iterator)

Attention:

In the iteration, it is not possible to manipulate the elements in the collection by means of the collection object because the concurrentmodificationexception exception (concurrency exception) occurs, so, at the time of the iterator, Can only use the method of the iterator artificial element because the iterator method is limited, it can only judge the elements, take out, delete the operation if you want other operations such as add, modify, etc., you need to use its sub-interface, Listiterator This interface can only be obtained through the Listiterator method of the list collection

 Public classListiteratordemo { Public Static voidMain (string[] args) {ArrayList A1=NewArrayList (); A1.add ("Java01"); A1.add ("Java02"); A1.add ("Java03"); A1.add ("Java04"); System.out.println ("The original collection is:" +A1); /*prepare to add or remove elements during an iteration Iterator it = Al.iterator ();        while (It.hasnext ()) {Object obj = It.next (); if (Obj.equals ("JAVA02"))//al.add ("java008"), or a concurrency exception occurs because the iterator is manipulating the collection and can no longer use the collection's methods to manipulate the collection it.remove ();//JAVA02 references from        The System.out.println ("obj:" +obj) was removed from the collection; }        */       //only list of listiterator have increased, delete, change, check these functions, because only the list has an indexListiterator Li =A1.listiterator ();  while(Li.hasnext ()) {if(Li.next (). Equals ("Java02"))          //Li.add ("java009");Li.set ("java006"); }       }}

Vector: Enumeration is a vector-specific way of fetching, much like an iterator (in fact, enumerations and iterations are the same) have been replaced by iterators

 Public class Vectordemo {     publicstaticvoid  main (string[] args) {         new  Vector ();         V.add ("JAVA01");         V.add ("JAVA02");         V.add ("java03");         V.add ("java04");           for (Enumeration en = v.elements (); en.hasmoreelements ();) {            System.out.println (en.nextelement ());        }    }} 

LinkedList:

Unique methods:

AddFirst (); Adding elements to the head

AddLast (); Adding elements at the tail

GetFirst (); GetLast ();

Gets the element but does not delete the element. If there are no elements in the collection, Nosuchelementexception appears

Removefirst (); Removelast ();

Gets the element but deletes the element. If there are no elements in the collection, Nosuchelementexception appears

There's an alternative approach in JDK1.6.

Offerfirst (); Offerlast ();

Peekfirst (); Peeklast (); Gets the element, but the element is not deleted. If there are no elements in the collection, NULL is returned

Pollfirst (); Polllast (); Gets the element, but the element is deleted. If there are no elements in the collection, NULL is returned

 Public class Linkedlistdemo {     publicstaticvoid  main (string[] args) {           New  LinkedList ();         Link.add ("JAVA01");         Link.add ("JAVA02");         Link.add ("java03");         Link.add ("java04");           while (! Link.isempty ()) {            System.out.println ((Link.removelast ()));     }}}

Java List Usage Code Analysis very detailed

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.