java集合中的Iterable接口的简介与运用

Iterable接口 (java.lang.Iterable) 是Java集合的顶级接口之一。Collection接口继承Iterable,所以Collection的所有子类也实现了Iterable接口。

一个实现Iterable接口的类可以使用新的for循环,下面是一个示例:

1 List list = new ArrayList();
2  
3 for(Object o : list){
4 //do something o;
5 }

Iterable 接口只有一个方法:

1 public interface Iterable<T> {
2   public Iterator<T> iterator();
3 }

怎么去实现Iterable接口以便可以使用新的for循环,在这篇文章中Implementing the Iterable Interface可以找到解释。

猜你喜欢

转载自blog.csdn.net/hgh813210/article/details/48134827