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()方法,为什么可以最后一步这样调用呢?
ArrayList
继承自AbstractList
类,AbstractList
类重写了List
接口的iterator()
Method:Which
Itr
是AbstractList
的内部类,实现了Iterator
interface.Isn’t there ArrayList? ?
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.