"Go" Java list usage examples

Source: Internet
Author: User
Tags addall

Transferred from: http://www.jb51.net/article/45660.htm

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 the 3, the angle of 2 can also have a 3) because the set 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

Increase:

1 boolean Add (int  Index, E Element)2boolean addall (index,collection)
1   Public Static voidList_add () {2ArrayList A1 =NewArrayList ();3A1.add ("Java");4A1.add ("PHP");//the elements in the list collection can be repeated5A1.add (". NET");6System.out.println ("original collection:" +A1);7A1.add (1, "Flash");8A1.add (0, "PS")); 9 System.out.println (A1);Ten  OneArrayList A2 =NewArrayList (); AA2.add ("JavaScript"); -A2.add ("3dMax"); -A2.add ("IBM"); the  -A1.addall (0, a2); - System.out.println (A1); -}
using the example
Original collection: [Java, PHP,. Net][ps, Java, Flash, PHP,. Net][javascript, 3dMax, IBM, PS, Java, Flash, PHP,. NET]
Run Results

Delete the element at the specified location:

boolean remove (int index)
1   Public Static voidList_remove () {2ArrayList A1 =NewArrayList ();3A1.add ("JavaScript");4A1.add ("PHP");5A1.add ("Flash");6System.out.println ("original collection:" +A1);7 8A1.remove (0);9 System.out.println (A1);Ten}
using the example
Original collection: [JavaScript, PHP, flash][php, Flash]
Run Results

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

1   Public Static voidList_set () {2ArrayList A1 =NewArrayList ();3A1.add ("JavaScript");4A1.add ("PHP");5A1.add (". NET");6System.out.println ("original collection:" +A1);7 8A1.set (1, "Falsh");9 System.out.println (A1);Ten}
using the example
Original collection: [JavaScript, PHP,. Net][javascript, Falsh,. NET]
Run Results

Check:

Get (int  index)   returns the element sublist (int int toindex) at the specified position in the list    Returns a partial element between the specified FromIndex (inclusive) and Toindex (not included) in the list.
1   Public Static voidList_get () {2ArrayList A1 =NewArrayList ();3A1.add ("Java");4A1.add ("PHP");5A1.add ("Flash");6 7System.out.println (a1.get (0));//Gets the element that specifies the corner label, which allows you to iterate through all the elements in the collection8 9System.out.println (A1.sublist (1, 3));//gets the element of a part of the collection that contains the header that does not contain a tailTen}
using the example
java[php, Flash]
Run Results

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

Attention:
When iterating, it is not possible to manipulate elements in a collection by means of a collection object
Because concurrentmodificationexception exceptions occur (concurrency exceptions)
So, in an iterator, you can only use the iterator method to do the 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 adding, modifying, and so on, you need to use their subinterfaces, listiterator
This interface can only be obtained through the Listiterator method of the list collection

1   Public classListiteratordemo {2       Public Static voidMain (string[] args) {3ArrayList A1 =NewArrayList ();4A1.add ("Java01");5A1.add ("Java02");6A1.add ("Java03");7A1.add ("Java04");8 9System.out.println ("Original collection is:" +A1);Ten  One       /*preparing to add or remove elements during an iteration A Iterator it = Al.iterator (); - While (It.hasnext ()) { - Object obj = It.next (); the  - if (obj.equals ("JAVA02")) - //al.add ("java008");//A concurrency exception occurs because the iterator is manipulating the collection and can no longer manipulate the collection using the collection's methods - it.remove ();//The reference to JAVA02 was removed from the collection + System.out.println ("obj:" +obj); -         } +         */       A      //only list of listiterator have increased, delete, change, check these functions, because only the list has an index atListiterator Li =a1.listiterator (); -            while(Li.hasnext ()) { -           if(Li.next (). Equals ("Java02")) -           //Li.add ("java009"); -Li.set ("java006"); -         }    in     } - } to  
using the example

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

1   Public classVectordemo {2       Public Static voidMain (string[] args) {3Vector v =NewVector ();4V.add ("Java01");5V.add ("Java02");6V.add ("Java03");7V.add ("Java04");8 9           for(Enumeration en =v.elements (); en.hasmoreelements ();) {Ten System.out.println (En.nextelement ()); One         } A     }   -  } -  
using the example

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

1   Public classLinkedlistdemo {2       Public Static voidMain (string[] args) {3LinkedList link =NewLinkedList ();4Link.add ("Java01");5Link.add ("Java02");6Link.add ("Java03");7Link.add ("Java04");8 9           while(!Link.isempty ()) {Ten System.out.println ((Link.removelast ())); One         } A     }   -  } -  
using the example
Java04java03java02java01
Run Results

"Go" Java list usage examples

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.