首页 > 其他 > 详细

Iterator和Iterable区别

时间:2020-03-23 18:28:42      阅读:80      评论:0      收藏:0      [点我收藏+]

Iterable接口下面只有一个方法——iterator

?

public interface Iterable

?

{

?

public abstract Iterator iterator();

?

}

?

?

Collection继承了Iterable接口

Iterator<E> iterator();

?

List继承Collection接口

ArrayList实现List接口

????public Iterator<E> iterator() {

????????return new Itr();

????}

?

返回一个迭代器对象,这个对象的实现类继承了Iterator接口

public interface Iterator

?

{

?

public abstract boolean hasNext();

?

public abstract Object next();

?

public abstract void remove();

?

}

?

????private class Itr implements Iterator<E>??{

????????int cursor;???????// index of next??element to return

????????int lastRet = -1; // index of last??element returned; -1 if no such

????????int expectedModCount = modCount;

????????// prevent creating a synthetic??constructor

????????Itr() {}

????????public boolean hasNext() {

????????????return cursor != size;

????????}

????????@SuppressWarnings("unchecked")

????????public E next() {

????????????checkForComodification();

????????????int i = cursor;

????????????if (i >= size)

????????????????throw new??NoSuchElementException();

????????????Object[] elementData =??ArrayList.this.elementData;

????????????if (i >= elementData.length)

????????????????throw new??ConcurrentModificationException();

????????????cursor = i + 1;

????????????return (E) elementData[lastRet =??i];

????????}

????????public void remove() {

????????????if (lastRet < 0)

????????????????throw new??IllegalStateException();

????????????checkForComodification();

????????????try {

?????????????????ArrayList.this.remove(lastRet);

????????????????cursor = lastRet;

????????????????lastRet = -1;

????????????????expectedModCount = modCount;

????????????} catch??(IndexOutOfBoundsException ex) {

????????????????throw new??ConcurrentModificationException();

????????????}

????????}

????????@Override

????????public void??forEachRemaining(Consumer<? super E> action)??{

????????????Objects.requireNonNull(action);

????????????final int size =??ArrayList.this.size;

????????????int i = cursor;

????????????if (i < size) {

????????????????final Object[] es =??elementData;

????????????????if (i >= es.length)

????????????????????throw new??ConcurrentModificationException();

????????????????for (; i < size && modCount??== expectedModCount; i++)

?????????????????????action.accept(elementAt(es, i));

????????????????// update once at end to??reduce heap write traffic

????????????????cursor = i;

????????????????lastRet = i - 1;

????????????????checkForComodification();

????????????}

????????}

????????final void checkForComodification()??{

????????????if (modCount != expectedModCount)

????????????????throw new??ConcurrentModificationException();

????????}

????}

?

另外for each,反编译后

Integer i;

for(Iterator iterator = list.iterator(); iterator.hasNext(); System.out.println(i)){

????i = (Integer)iterator.next();???

??}

其实就是依赖Iterator迭代器

(只要实现了Iterable接口,就可以直接使用foreach)

?

?

技术分享图片

一个小问题: 为什么不直接将hasNext()next()方法放在Iterable接口中,其他类直接实现就可以了??

答: 原因是有些集合类可能不止一种遍历方式,实现了Iterable的类可以再实现多个Iterator内部类,例如LinkedList中的ListItrDescendingIterator两个内部类,就分别实现了双向遍历和逆序遍历。通过返回不同的Iterator实现不同的遍历方式,这样更加灵活。如果把两个接口合并,就没法返回不同的Iterator实现类了。


?

Iterator和Iterable区别

原文:https://www.cnblogs.com/beeenwei/p/12553639.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!