问答详情
源自:4-6 学生选课—课程查询

通过迭代器出错?

public void testIterator() {
  //通过集合的iterator方法,取得迭代器的事例
  
   Iterator it = (Iterator) coursesToSelect.iterator();
   System.out.println("有如下课程待选(通过迭代器访问):");
   while(it.hasNext()) {
    Course cr =(Course)it.next();
    System.out.println("课程:" + cr.id+ ":" + cr.name);
   }
  }
 
 

提问者:qq_匆匆那年_47 2018-09-09 10:10

个回答

  • 慕码人5393438
    2018-10-16 14:01:17

    引入的包错了,iterator在多个包中都有时实现,你引入的是HTMLDocument.Iterator,正确的应该是:java.util.Iterator


  • qq_匆匆那年_47
    2018-09-09 10:13:18

    报错

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
     The method hasNext() is undefined for the type HTMLDocument.Iterator
     Type mismatch: cannot convert from void to Course

     at com.imooc_collection.ListTest.testIterator(ListTest.java:90)
     at com.imooc_collection.ListTest.main(ListTest.java:113)