您的位置:首页 > 编程语言 > Java开发

thinking java

2015-10-03 10:07 525 查看
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的作用:能够将遍历序列的 操作与存储序列的容器结构分离。由此,可以说迭代器统一了对容器的访问方式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: