Package cn.itcast_03;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
/*
* Store custom objects and traverse, combined with normal for loop (size () and get ())
*/
public class ListDemo3 {
public static void Main (string[] args) {
To create a collection object
List List = new ArrayList ();
Create Student objects
Student S1 = new Student ("Lin Daiyu", 18);
Student s2 = new Student ("Granny Liu", 88);
Student s3 = new Student ("Wang Xifeng", 38);
Add students to the collection
List.add (S1);
List.add (S2);
List.add (S3);
Traverse
iterator traversal
Iterator it = List.iterator ();
while (It.hasnext ()) {
Student s = (Student) it.next ();
System.out.println (S.getname () + "---" + s.getage ());
}
System.out.println ("--------");
normal for Loop
for (int x = 0; x < list.size (); + +) {
Student s = (Student) list.get (x);
System.out.println (S.getname () + "---" + s.getage ());
}
}
}
This article from "GD" blog, reproduced please contact the author!
Collection Framework (List stores custom objects and traverses)