The 1.List interface provides a common method for itself that is related to the index, because the list collection is the type of the lists, the objects are stored linearly, and objects can be manipulated through the object's index.
The common implementation classes of the list interface are ArrayList and LinkedList, which, when using the list collection, are typically declared as a list type, instantiated as required by the actual situation, and instantiated as
ArrayList or LinkedList, for example:list<string> L = new arraylist<string> ();//Instantiate List collection with ArrayList class list< string> L2 = new linkedlist<string> ();
Instantiate a list collection with the LinkedList class
2. The difference between the Add (int index, Object obj) method and the set (int index, Object obj) method requires attention when using the list collection to distinguish the Add (int index, Object obj) method and set (int index, O Bject obj) method,
The former is to add an object to the specified index location, which is the object that modifies the specified index position
The difference between the 3.indexOf (object obj) method and the LastIndexOf (object obj) method requires attention to distinguish between the IndexOf (object obj) method and the LastIndexOf (object obj) when using the list collection Method
The former is the smallest index position to get the specified object, and the latter is the largest index position for the specified object, provided that the specified object has duplicate objects in the list collection, otherwise if in the list collection
Has and has only one specified object, the index position obtained by these two methods is the same
The 4 sublist (int fromIndex, int toindex) method uses the sublist (int fromIndex, int toindex) method to intercept some objects in an existing list collection when generating a new list collection, it is important to note that
The newly generated collection contains the object represented by the starting index position, but does not contain the object represented by the terminating index position
Expand:
- Usual usage:list< type > list=new arraylist< type > ();
- List is an interface that cannot be instantiated,
- By instantiating its implementation class to use the list collection,
- His most commonly implemented class of ArrayList;
- Use example:list<string> list= new arraylist<string> ();
- list<t> list=New arraylist<t> ();
- Where type T is a constraint on the list collection element type,
- For example, you declare a list<string>
- Then add an object that is not of type string to this set.
- An exception is thrown.
four ways to traverse ArrayList:
Package com.test;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
public class Arraylistdemo {
publicstatic void Main (String args[]) {
list<string> list = newarraylist<string> ();
List.add ("Luojiahui");
List.add ("Luojiafeng");
Method 1
Iterator it1 = List.iterator ();
while (It1.hasnext ()) {
System.out.println (It1.next ());
}
Method 2
for (Iterator it2 = List.iterator (); It2.hasnext ();) {
System.out.println (It2.next ());
}
Method 3
for (String tmp:list) {
SYSTEM.OUT.PRINTLN (TMP);
}
Method 4
for (int i = 0;i < List.size (); i + +) {
System.out.println (List.get (i));
}
}
}
Java list and ArrayList four ways to traverse