The list interface inherits from the collection interface
has three major characteristics:
1. ordered set : the same order of deposit and withdrawal
2. users of this interface can have precise control of where each element in the list is inserted : can be manipulated by index
3. can store duplicate elements
Unique methods for the list interface (related to indexes):
Packagedemo;Importjava.util.List;Importjava.util.ArrayList;//The list interface inherits the collection interface, and there are many implementation classes//such as ArrayList Public classListdemo { Public Static voidMain (string[] args) {function1 ();//AddFunction2 ();//DeleteFunction3 ();//Modify } Public StaticList<string>function () {//Create a collectionlist<string> list =NewArraylist<>(); List.add ("ABC1"); List.add ("ABC2"); List.add ("ABC3"); List.add ("ABC4"); returnlist; } Public Static voidfunction1 () {List<String> list =function (); SYSTEM.OUT.PRINTLN (list); //[ABC1, ABC2, ABC3, ABC4]List.add (1, "abc");//adding on the 1 indexSystem.out.println (list); //[ABC1, ABC, ABC2, ABC3, ABC4] } Public Static voidfunction2 () {List<String> list =function (); SYSTEM.OUT.PRINTLN (list); //[ABC1, ABC2, ABC3, ABC4]String s = list.remove (1); System.out.println (s);//Output: ABC2System.out.println (list); //[ABC1, ABC3, ABC4] } Public Static voidFunction3 () {List<String> list =function (); SYSTEM.OUT.PRINTLN (list); //[ABC1, ABC2, ABC3, ABC4]String s = list.set (2, "ABC"); System.out.println (s);//Output: ABC3System.out.println (list); //[ABC1, ABC2, ABC, ABC4] }}
There are three ways to traverse the list collection: iterator traversal, normal for loop traversal, enhanced for loop traversal
Data storage structure for the list interface:
Many of the sub-class storage elements in the list are structured differently, resulting in many sets having their own characteristics:
The common structure of data storage is: Stack, queue, array, list, here is a brief introduction:
Stack storage structure Popular explanation: Bullets pressed into the magazine, the first pressure in the bottom, after the pressure in the top, when shooting, the top of the bullets first bounce out, the bottom of the back out (advanced)
Queue storage Structure Popular explanation: Take the train line security, everyone in turn check, only the front of the people to check, can turn to the back of the person (first-out)
Array structure: Has been contacted many times, through the index to find the block, but because of the fixed length, so adding and deleting slow
Linked list structure: Each element is divided into two parts, a part records the previous element address, and the other part stores information about the current element so that each element is connected like a bicycle chain
Look for the time due to the need to connect the node, slow, but add and subtract, the principle is the direct operation of the address, does not change its structure, so fast
ArrayList collection uses the array structure storage method, so query fast, adding and deleting slow, because the thread is unsafe, running fast,
Null parameter Create initial capacity 10, variable group, principle is the system of the copy array method, and some other operations after the expansion
The LinkedList collection uses the data structure of the one-way linked list, so the deletion is fast, the query is slow, the same thread is not synchronized, and the running speed is fast.
LinkedList provides a large number of end-to-end operations
Example:
Packagedemo;Importjava.util.LinkedList; Public classLinkedlistdemo { Public Static voidMain (string[] args) {function1 ();//AddFunction2 ();//Get KinsokuFunction3 ();//Remove and return to the beginning } Public Static voidfunction1 () {LinkedList<String> link =NewLinkedlist<string>(); Link.addlast (A); Link.addlast ("B"); Link.addlast (C); Link.addlast ("D"); Link.addfirst ("1"); Link.addfirst ("2"); Link.addfirst ("3"); System.out.println (link); //[3, 2, 1, a, B, C, d] } Public Static voidfunction2 () {LinkedList<String> link =NewLinkedlist<string>(); Link.add ("1"); Link.add ("2"); Link.add ("3"); Link.add ("4"); if(!Link.isempty ()) {String First=Link.getfirst (); String Last=Link.getlast (); SYSTEM.OUT.PRINTLN (first);//1System.out.println (last);//4 } } Public Static voidFunction3 () {LinkedList<String> link =NewLinkedlist<string>(); Link.add ("1"); Link.add ("2"); Link.add ("3"); Link.add ("4"); String First=Link.removefirst (); String Last=Link.removelast (); SYSTEM.OUT.PRINTLN (first);//1System.out.println (last);//4System.out.println (link);//[2, 3] }}
There is also a vector collection, the array structure, is the JDK's earliest collection, the method and ArrayList basically the same, the running speed is slow, so later by the ArrayList instead, here does not introduce
Java Learning Note 30 (Set frame four: List interface)