Collection c1 =new ArrayList();
c1.add("hello");
c1.add(1);
Collection c2 =new ArrayList();
c2.add(new Student() );
c2.addAll(c1);
c2.add("我");
Iterator it = c2.iterator();
Collection 与List 还有ArrayList都没有重写iterator()方法,为什么可以最后一步这样调用呢?
高洛峰2017-04-18 10:49:56
ArrayList
继承自AbstractList
类, AbstractList
类重写了List
接口的iterator()
Method:
public Iterator<E> iterator() {
return new Itr();
}
Which Itr
是AbstractList
的内部类,实现了Iterator
interface.
天蓬老师2017-04-18 10:49:56
This Iterator should be the internal class of ArrayList. If it cannot be found in ArrayList, it should be in its base class. Iterators should be different for different collection classes. You can look at the source code yourself.