Java list usage examples detailed _java

Source: Internet
Author: User
Tags addall array length

|--list: Elements are ordered (how to save how to get out, the order will not be disorderly), elements can be repeated (1 on the corner of a 3, 2 can also have a 3) because the collection system has an index,
|--ArrayList: The underlying data structure uses array structures (the array length is a variable 50% extension) (the feature is that the query is quick, but the additions and deletions are slow) the thread is not synchronized
|--LinkedList: The bottom of the data structure is linked to the list of structures (characterized by slow query, additions and deletions faster)
|--Vector: The bottom is the array data structure thread synchronization (array length is a variable 100% extension) (regardless of query or deletion are very slow, by ArrayList replaced)

List: Unique method, the method that can operate the angle standard is the system peculiar method

Increase

Copy Code code as follows:

Boolean Add (int index, E Element)
Boolean AddAll (Index,collection)

Copy Code code as follows:

public static void List_add () {
ArrayList A1 = new ArrayList ();
A1.add ("Java");
A1.add ("PHP");//list the elements in the collection can be repeated
A1.add (". NET");
System.out.println ("Original set:" +A1);
A1.add (1, "Flash");
A1.add (0, "PS");
SYSTEM.OUT.PRINTLN (A1);

ArrayList A2 = new ArrayList ();
A2.add ("JavaScript");
A2.add ("3dMax");
A2.add ("IBM");

A1.addall (0, A2);
SYSTEM.OUT.PRINTLN (A1);
}


Deletes the element at the specified location

Copy Code code as follows:

Boolean remove (int index)

Copy Code code as follows:

public static void List_remove () {
ArrayList A1 = new ArrayList ();
A1.add ("JavaScript");
A1.add ("PHP");
A1.add ("flash");
System.out.println ("Original set:" +A1);

A1.remove (0);
SYSTEM.OUT.PRINTLN (A1);
}


Modify the element set (int index, E Element) of the specified corner to return the modified element

Copy Code code as follows:

public static void List_set () {
ArrayList A1 = 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

Copy Code code as follows:

Get (int index) returns the element at the specified position in the list
sublist (int fromindex, int toindex) returns a portion of the element between the Fromindex (including) and Toindex (not included) specified in the list.

Copy Code code as follows:

public static void List_get () {
ArrayList A1 = new ArrayList ();
A1.add ("Java");
A1.add ("PHP");
A1.add ("flash");

System.out.println (a1.get (0))//Gets the element of the specified corner, and with this method you can iterate through all the elements in the collection

System.out.println (A1.sublist (1, 3))//Gets the element of a part of the collection containing the header without the tail
}

Iterator specific to the list collection: Listiterator (is a iterator sub-interface)

Attention:
In iterations, it is not possible to manipulate elements in a collection through the collection object's methods
Because a concurrentmodificationexception exception (a concurrency exception) can occur
So, in an iterator, you can only use an iterator's method to make an element
Because the iterator method is limited, you can only judge the elements, take out, delete the operation
If you want other actions such as add, modify, etc., you need to use its sub-interfaces, listiterator
This interface can only be obtained through the Listiterator method of the list collection

Copy Code code as follows:

public class Listiteratordemo {
public static void Main (string[] args) {
ArrayList A1 = new ArrayList ();
A1.add ("Java01");
A1.add ("Java02");
A1.add ("java03");
A1.add ("Java04");

System.out.println ("Original set is:" +A1);

/* Prepare to add or remove elements during an iterative process
Iterator it = Al.iterator ();
while (It.hasnext ()) {
Object obj = It.next ();

if (Obj.equals ("JAVA02"))
Al.add ("java008");//A concurrency exception occurs because the iterator is manipulating the collection and cannot use the collection's method operation to set the
It.remove ()//To remove JAVA02 references from the collection
System.out.println ("obj:" +obj);
}
*/

Only the list of Listiterator has been added, deleted, changed, check these functions, because only the list has an index
Listiterator li = A1.listiterator ();
while (Li.hasnext ()) {
if (Li.next (). Equals ("Java02"))
Li.add ("java009");
Li.set ("java006");
}
}
}

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

Copy Code code as follows:

public class Vectordemo {
public static void Main (string[] args) {
Vector v = 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 element AddLast () to the head, adding elements to the tail

GetFirst (); GetLast (); Gets the element but does not delete the element. If there are no elements in the collection, Nosuchelementexception will appear

Removefirst (); Removelast (); Gets the element but deletes the element. If there are no elements in the collection, Nosuchelementexception will appear

There's a workaround in JDK1.6.

Offerfirst (); Offerlast ();

Peekfirst (); Peeklast (); Gets the element, but the element is not deleted. Returns null if there are no elements in the collection

Pollfirst (); Polllast (); Gets the element, but the element is deleted. Returns null if there are no elements in the collection

Copy Code code as follows:

public class Linkedlistdemo {
public static void Main (string[] args) {
LinkedList link = new LinkedList ();
Link.add ("Java01");
Link.add ("Java02");
Link.add ("java03");
Link.add ("Java04");

while (!link.isempty ()) {
System.out.println ((Link.removelast ()));
}
}

}

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.