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()方法,為什么可以最后一步這樣調(diào)用呢?
歡迎選擇我的課程,讓我們一起見證您的進步~~
ArrayList
繼承自AbstractList
類, AbstractList
類重寫了List
接口的iterator()
Method:
public Iterator<E> iterator() {
return new Itr();
}
Which Itr
是AbstractList
的內(nèi)部類,實現(xiàn)了Iterator
interface.
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.