关于Iterator Collection

leyestd 2012-08-23 08:34:29
public static void display(Iterator<Pet> it) {
while(it.hasNext()) {
Pet p = it.next();
System.out.print(p.id() + ":" + p + " ");
}
System.out.println();
}
public static void display(Collection<Pet> pets) {
for(Pet p : pets)
System.out.print(p.id() + ":" + p + " ");
System.out.println();
}
Collection<Pet>超类不是Iterator吗
乐叶 8:33:46
为何List<Pet> petList = Pets.arrayList(8);
display(petList);
把public static void display(Collection<Pet> pets)删了就不行
...全文
85 4 打赏 收藏 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
菖蒲老先生 2012-08-23
  • 打赏
  • 举报
回复

public static void display(Iterable<Pet> iterable) {
Iterator<Pet> it = iterable.iterator();
while (it.hasNext()) {
Pet p = it.next();
System.out.print(p.id() + ":" + p + " ");
}
System.out.println();
}
scbb 2012-08-23
  • 打赏
  • 举报
回复
Collection接口的超类是Iterable (可迭代的)

而不是Iterator (迭代器)
nmyangym 2012-08-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
Collection的定义:
public interface Collection<E>
extends Iterable<E>

Collection接口的超类是Iterable,而不是Iterator
[/Quote]
+1
petList 显然不是一个迭代器Iterator对象,所以报错。
rdchris 2012-08-23
  • 打赏
  • 举报
回复
Collection的定义:
public interface Collection<E>
extends Iterable<E>

Collection接口的超类是Iterable,而不是Iterator

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧