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);
}
}
引入的包错了,iterator在多个包中都有时实现,你引入的是HTMLDocument.Iterator,正确的应该是:java.util.Iterator
报错
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)