什么是迭代器和集合?这两个有关系吗?
// the interface definition
Interface Iterator {
boolean hasNext();
Object next(); // note "one-way" traffic
void remove();
}
// an example
public static void main (String[] args){
ArrayList cars = new ArrayList();
for (int i = 0; i < 12; i++)
cars.add (new Car());
Iterator it = cats.iterator();
while (it.hasNext())
System.out.println ((Car)it.next());
}
Run Code Online (Sandbox Code Playgroud)
Interface Iterator是否单独预定义了这些方法名称或用户定义了哪些方法名称?下面这四行实际上讲的是什么?
cars.add (new Car());
Iterator it = cats.iterator();
while (it.hasNext())
System.out.println ((Car)it.next());
Run Code Online (Sandbox Code Playgroud)
谢谢.我正在阅读一本收藏的书.