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)删了就不行

解决方案 »

  1.   

    Collection的定义:
    public interface Collection<E>
    extends Iterable<E>Collection接口的超类是Iterable,而不是Iterator
      

  2.   

    +1
    petList 显然不是一个迭代器Iterator对象,所以报错。
      

  3.   

    Collection接口的超类是Iterable (可迭代的)而不是Iterator (迭代器)
      

  4.   


    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();
    }