|
Package com.lvsling.test;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
Public class testarraylist {
Public staticvoid main (string[] args) {
//student-- There are three properties, name,age,clazz
list<student> list = new arraylist<student> ();
Student S1 = new Student ("Jerry", "1");
List.add (S1);
List.add (new Student ("Tom", "2"));
List.add (new Student ("Mike", "1"));
// Subscript Traversal
For (int i=0; i<list.size (); i++) {
System. out. println (List.get (i));
}
System. out. println ("-------------------------");
//ForEach Traverse
For (Object obj:list) {
Student s = (Student) obj;
System. out. println (S.getname ());
}
System. out. println ("-------------------------");
// Iterative Traversal
Iterator it = List.iterator (); // gets the iterator for the collection object
While (It.hasnext ()) { // Determines if there is a next element
Object obj = It.next (); // Get Next element
System. out. println (obj);
}
}
}
|