迭代器(Iterator)是一种设计模式。它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构。在JAVA的Collection框架中已经实现,它提供了这几个接口方法:
/*
* @(#)Iterator.java 1.27 06/07/24
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.util;
public interface Iterator<E> {
boolean hasNext();
E next();
void remove();
}
package java.util;
public interface Collection<E> extends Iterable<E> {
Iterator<E> iterator();
//等等代码
}
这里可以看见,在Collection接口中是继承Iterable这个接口的。并且提供了一个Iterator<E> iterator();
我们继续看LIST容器接口:
package java.util;
public interface List<E> extends Collection<E> {
// .......
}
从这里可以看见,这个接口也是继承于Collection的,所以自然也就拿到了Iterable。以上都是接口,下面来看看实现类:
package java.util;
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
//..............
}
再看看AbstractList的实现类吧。
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
public Iterator<E> iterator() {
return new Itr();
}
//...............
}
终于找到这个iterator()方法了,看它实现了什么,return new Itr()?不要奇怪这种写法,这里的 private class Itr 是个内部类。
private class Itr implements Iterator<E> {
/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0;
/**
* Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1;
/**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int expectedModCount = modCount;
public boolean hasNext() {
return cursor != size();
}
public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public E previous() {
checkForComodification();
try {
int i = cursor - 1;
E previous = get(i);
lastRet = cursor = i;
return previous;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor-1;
}
public void set(E e) {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.set(lastRet, e);
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(E e) {
checkForComodification();
try {
AbstractList.this.add(cursor++, e);
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}
顺便来说说这个设计,很明显使用了策略模式,但是其中还使用了些抽象。
好了,看了原理,到这也就知道个大概了。也就不继续去深入下去了。我们来玩一下:
package moshi.iterator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class iter {
public static void main(String[] args) {
List list=new ArrayList();
list.add("a");
list.add("b");
list.add("c");
Iterator itr=list.iterator();
while(itr.hasNext()){
String str = (String) itr.next();
System.out.println(str);
}
}
}
PASS一下:这几个方法:hasNext()是否有下一个存在。next()下一个值。iterator()返回一个Iterator来使用。
当然,JAVA的强大何于满足一种WAY?看看下面的代码。
package moshi.iterator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class iter {
public static void main(String[] args) {
List<String> list=new ArrayList();
list.add("a");
list.add("b");
list.add("c");
for(String newList:list){
System.out.println(newList);
}
}
}
引用
输出:
a
b
c
引用
Iterator模式:提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露内部的表示。因为JAVA内库中已经帮我们实现了,所以我们也就不要去实现Iterator了,但是记得要使用Collection容器框架哦。呵呵呵!看了这些继承,接口,实现乱七八糟的东西,您现在对Iterator还费解不?