List usage
List includes all the implementation classes of the List interface and List interface. Because the List interface implements the Collection interface, the List interface has all the common methods provided by the Collection interface. Because List is a List type, the List interface also provides some common methods suitable for itself, see table 1.
Table 1 common methods and functions defined by the List Interface
From table 1, we can see that the common methods provided by the List interface are related to indexes. This is because the List set is of the List type and objects are stored linearly, you can use the index of an object to operate an object.
Common Implementation classes of the List interface include ArrayList and listlist. When using the List set, it is usually declared as the List type. During instantiation, It is instantiated as the ArrayList or listlist according to the actual needs, for example:
List <String> l = new ArrayList <String> (); // use the ArrayList class to instantiate a List set.
List <String> l2 = new consumer List <String> (); // use the consumer List class to instantiate a List set.
1. Differences between the add (int index, Object obj) method and the set (intindex, Object obj) Method
When using the list set, note that the add (INT index, object OBJ) method and set (INT index, object OBJ) method are distinguished. The former is to add an object to the specified index location, the latter is to modify the object at the specified index location. For example, execute the following code:
Public static void main (String [] args) {String a = "A", B = "B", c = "C", d = "D ", e = "E"; List <String> list = new external list <String> (); List. add (a); list. add (e); list. add (d); list. set (1, B); // Modify Object e with index position 1 to object blist. add (2, c); // add Object c to the Iterator <String> it = list at the index location of 2. iterator (); while (it. hasNext () {System. out. println (it. next ());}}
The following information is output in the console:
A
B
C
D
Because the List set can access objects through the index location, you can also traverse the List set through the for loop. for example, the Code for traversing the List set in the above Code is as follows:
For (INT I = 0; I <list. size (); I ++) {system. out. println (list. get (I); // get the object at the specified index position using the get (INT index) method} SRC \ com \ mwq \ testcollection. the complete Java code is as follows: Package COM. mwq; import Java. util. arraylist; import Java. util. using list; import Java. util. iterator; import Java. util. list; public class testcollection {public static void main (string [] ARGs) {system. out. println ("START:"); string a = "A", B = "B", c = "C", D = "D", E = "E"; List <string> List = new external list <string> (); list. add (a); list. add (E); list. add (d); list. set (1, B); // Modify Object E with index position 1 to object blist. add (2, c); // Add Object C to the iterator <string> it = List at the index location of 2. iterator (); While (it. hasnext () {system. out. println (it. next ();} // For (INT I = 0; I <list. size (); I ++) {// system. out. println (list. get (I); // use the get (INT index) method to obtain the object at the specified index position //} system. out. println ("end! ");}}
2. Differences between the indexof (Object OBJ) method and the lastindexof (objectobj) Method
When using the list set, you must note that the indexof (objectobj) method and the lastindexof (Object OBJ) method are distinguished. The former is to obtain the smallest index location of the specified object, the latter is to obtain the maximum index location of the specified object. The precondition is that the specified object has duplicate objects in the list set. Otherwise, if there is only one specified object in the list set, the index location obtained through these two methods is the same, for example, execute the following code:
Package COM. mwq; import Java. util. arraylist; import Java. util. list; public class testcollection {public static void main (string [] ARGs) {system. out. println ("START:"); string a = "A", B = "B", c = "C", D = "D", repeat = "repeat "; list <string> List = new arraylist <string> (); list. add (a); // The index position is 0list. add (Repeat); // The index location is 1list. add (B); // The index location is 2list. add (Repeat); // The index position is 3list. add (c); // The index position is 4list. add (rep Eat); // The index position is 5list. add (d); // The index location is 6system. out. println (list. indexof (Repeat); system. out. println (list. lastindexof (Repeat); system. out. println (list. indexof (B); system. out. println (list. lastindexof (B); system. out. println ("end! ");}}
The following information is output in the console:
1
5
2
2
3. subList (int fromIndex, int toIndex) Method
When using the subList (int fromIndex, int toIndex) method to intercept some objects in an existing List set to generate a new List set, note that, the newly generated collection contains the Objects represented by the starting index location, but does not contain the Objects represented by the ending index location. For example, run the following code:
Package com. mwq; import java. util. arrayList; import java. util. list; public class TestCollection {public static void main (String [] args) {System. out. println ("START:"); String a = "A", B = "B", c = "C", d = "D", e = "E "; list <String> list = new ArrayList <String> (); list. add (a); // The index position is 0list. add (B); // The index position is 1list. add (c); // The index location is 2list. add (d); // The index position is 3list. add (e); // The index position is 4 list = list. subList (1, 3); // generates a List set for (int I = 0; I <list. size (); I ++) {System. out. println (list. get (I ));}
Any object can be added to the list. I can give you an example:
Class Person {
.....
}
The Person class is defined above. The following describes how to use List
Person p1 = new Person (); Person p2 = new Person (); List list = new ArrayList (); list. add (p1); list. add (p2); // add the object to the list for (int I = 0; I <list. size (); I ++) {// take all the person objects out of the Person p = (Person) list in a loop. get (I); // note that type conversion must be performed here, because the objects retrieved in the List are of the Object type, and hope to help you}