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()方法,为什么可以最后一步这样调用呢?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
ArrayList继承自AbstractList类,AbstractList类重写了List接口的iterator()方法:其中
Itr是AbstractList的内部类,实现了Iterator接口。ArrayList不是有吗??
这个Iterator应该是ArrayList的内部类吧,如果ArrayList中找不到的话它的基类中应该有,不同的集合类Iterator应该也各不相同,你可以自己看看源码。