List的迭代器使用
- Interable接口
public interface Iterable<T> {
Iterator<T> iterator();
}
- List类继承了该接口,所以有iterator()方法,用于返回一个Iterator对象(接口继承接口不用implements而用extends)
public interface List<T> extends Iterable<T>{
Iterator<T> iterator();
...
}
- Iterator对象有如下功能:
package java.util;
public interface Iterator<T> {
boolean hasNext();
T next();
}
- 类内的Iterator具体实现:
public class ArrayList<T> implements List