为什么API中ArrayList中没有Iterator方法
来源:百度知道 编辑:UC知道 时间:2024/05/23 12:37:12
如题
看了一下中文API
public class ArrayList<E>extends AbstractList<E>implements List<E>, RandomAccess, Cloneable, Serializable
ArrayList从AbstractList继承,而AbstractList中有iterator方法:
public Iterator<E> iterator()
这样自然就继承了AbstractList中的iterator方法
ArrayList中本来就没有 Iterator方法,因为你没有实现接口Iterator,而你说的“Iterator方法” 是不是指的hasnext(),next()方法,望楼主把Iterator迭代器真实的意思搞清楚了
这是因为iterator的定义是在List这个接口中,而不是ArraList中。
List接口下的ArrayList,LinkedList和Vector都要实现这个方法。
而对于ArrayList,它继承了AbstractList类,AbstractList这个类中定义了List接口的iterator方法,所以就没有在ArrayList中定义。
3楼正解!
有的。所有的Collection下的都有这个Iterator。去ArrayList的父类中找!