1. You can add any object to the List, including new classes you define.
-
class Person{ ..... List Person p1=new Person(); Person p2=new Person(); List list=new ArrayList(); list.add(p1); list.add(p2); For (int I =0; i Person p=(Person)list.get(i); // The List must be cast, because all the objects in the List are of type Object.Copy the code
MyList = new ArrayList(); myList = new ArrayList(); Add (any object); I'm ready to add. Mylist.get (index); The retrieved values are all Object, which requires type conversion. 5. Use the Iterator to iterate over elements in a List.Copy the code
Copy the code
The objects in the List collection are arranged in a certain order, and the contents can be repeated. The List interface implements the class: ArrayList(ArrayList), Vector (ArrayList), LinkedList (ArrayList), Stack (ArrayList) Add () is the most commonly used method. For other methods, please refer to the API help documentation. This method is used to add objects to a collection, and these objects are arranged in a certain order. Its internal principle is array implementation, so it is not recommended when dealing with a large amount of data. Public class TestArrayList {public static void main(String[] args) {// Declare List and instantiate as ArrayList List al = new ArrayList(); // add element al. Add ("a") with the add() method; al.add("b"); al.add("c"); al.add("d"); // Use Iterator to iterate over the elements of the set and print for(Iterator I = al.iterator(); i.hasNext(); ) { String str = (String) i.next(); System.out.println(str); }} the java.util.Vector class implements a dynamic array similar to ArrayList. Public class TestArrayList {public static void main(String[] args) {// Declare the List and instantiate it as Vector List al = new Vector(); // use the add() method to add the object.add ("a"); al.add("b"); al.add("c"); al.add("d"); For (Iterator I = al.iterator(); for(Iterator I = al.iterator(); i.hasNext(); ) { String str = (String) i.next(); System.out.println(str); The java.util.linkedList class implements linked lists that can be initialized to empty or existing collections, usually using the add() method; Add objects to the end of the list. AddFirst () adds objects at the beginning of the list. AddLast () adds objects to the end of the list. GetFirst () gets the object at the beginning of the list. GetLast () gets the object at the end of the list. Note that this class provides a way to randomly access elements in a list, but the underlying layer still has to traverse to find randomly accessed objects, Public static void main(String[] args) {// Declare LinkedList and instantiate LinkedList al = new LinkedList(); // add element al. Add ("a") with the add() method; al.add("b"); al.add("c"); al.add("d"); // Use Iterator to iterate over the elements of the set and print for(Iterator I = al.iterator(); i.hasNext(); ) { String str = (String) i.next(); System.out.println(str); } System.out.println("_____"); AddFirst ("z"); // Add x and z to the start and end of the list. al.addLast("x"); Iterator for(Iterator I = al.iterator(); i.hasNext(); ) { String str = (String) i.next(); System.out.println(str); }} the java.util.Stack class implements the Stack data structure, which stores data on a first-in, last-out basis. This parameter can only be null when it is created. Public static void main(String[] args) {// Declare Stack and instantiate Stack al = new Stack(); // Add element a.push ("a") with push(); al.push("b"); al.push("c"); al.push("d"); al.push("f"); // Use Iterator to iterate over the elements of the set and print for(Iterator I = al.iterator(); i.hasNext(); ) { String str = (String) i.next(); System.out.println(str); }}Copy the code
-----------
Copy the code
Example added by myself:Copy the code
public class Test {
Copy the code
public static void main(String[] args) { // TODO Auto-generated method stub ArrayList list = new ArrayList(); list.add(0,"aa"); list.add(1,"bb"); list.add(2,"cc"); for(int i=0; i<10; i++) { list.add(Integer.toString(i)); } for(int i=0; i<list.size(); i++) { System.out.println(list.get(i)); } for(Iterator i=list.iterator(); i.hasNext();) {String s =(String)i.next(); // Define an Iterator for a list object. System.out.println(s); }}Copy the code
}
Copy the code