黑马程序员技术交流社区
标题:
java程序
[打印本页]
作者:
せR3n、何必装纯
时间:
2011-11-11 10:41
标题:
java程序
Java code
import java.util.Iterator;
import java.util.NoSuchElementException;
public abstract class AbstractIterator<T> implements Iterator<T> {
T next = nextElement();
public boolean hasNext() {
return next != null;
}
public T next() {
if (next == null) {
throw new NoSuchElementException();
}
T result = next;
next = nextElement();
return result;
}
public void remove() {
throw new UnsupportedOperationException();
}
protected abstract T nextElement();
private static Iterator<Character> test(final String s) {
return new AbstractIterator<Character>() {
private int cursor = 0;
protected Character nextElement() {
return cursor == s.length() ? null : s.charAt(cursor++);
}
};
}
public static void main(String[] args) {
for (Iterator<Character> i = test("OPS"); i.hasNext();) {
System.out.print(i.next());
}
}
}
问题:输出结果是什么?
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2