Iterable接口 (java.lang.Iterable) 是Java集合的顶级接口之一,Iterable接口中只包含一个方法,就是iterator()方法,用来返回一个Iterator类型的对象,或者说返回一个实现了Iterator接口的对象。实现了Iterable接口的类拥有增强的for循环,即只要实现了Iterable接口的类,就可以使用Iterator迭代器了。
Iterator包含3个方法: hasNext , next , remove。
1、每次在迭代前,先调用hasNext()判断是否迭代到终点 (本次还能再迭代吗?);
2、next()方法不仅要返回当前元素,还要后移游标cursor;
3、remove()方法用来删除最近一次已经迭代出的元素。
资源链接:
1、Java中Iterable和Iterator的辨析 https://blog.csdn.net/lipengcn/article/details/51700153
2、Java迭代:Iterable和Iterator接口 https://www.cnblogs.com/hongwz/p/5510092.html