public class CrossContainerIteration{
public static void display(Iterator<Pet> it){
while(it.hasNext()){
Pet tmp = it.next();
System.out.print("输出:"+tmp.id());
}
}
public static void main(String[] args){
ArrayList<Pet> pets = Pets.arrayList(8);
LinkedList<Pet> petsLL = new LinkedList<Pet>(pets);
HashSet<Pet> petsHS = new HashSet<Pet>(pets);
TreeSet<Pet> petsTS = new TreeSet<Pet>(pets);
display(pets.iterator());
display(petsLL.iterator());
display(petsHS.iterator());
display(petsTS.iterator());
}
}
1. Iterator的作用:能够将遍历序列的 操作与存储序列的容器结构分离。由此,可以说迭代器统一了对容器的访问方式。