深入了解需要精读Java Generics and Collections
泛读材料40 Java Collections Interview Questions and Answers
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ListTest {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Robin");
list.add("TianYa");
list.add("Generic");
//Three Way Iterator List
System.out.println("***For Loop***");
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("***Foreach Loop***");
for (String n : list) {
System.out.println(n);
}
System.out.println("***Iterator***");
for (Iterator<String> it = list.iterator(); it.hasNext(); ) {
System.out.println(it.next());
}
//List Reference Object
List<Object> listObjcet = new ArrayList<>();
listObjcet.add(new PersonInfo(1, "Robin", "Java"));
listObjcet.add(new PersonInfo(2, "Tianya", "C++"));
listObjcet.add(new PersonInfo(3, "Collection", "C"));
System.out.println("***For Loop Object***");
for (Iterator<Object> it = listObjcet.iterator(); it.hasNext(); ) {
System.out.println(it.next());
}
}
}
展开阅读