为什么要写这样的迭代器呢? [英] Why bother to write an iterator like this?
问题描述
我正在阅读关于内部类的Java教程
I was reading through the Java tutorial on inner class
http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
它解释了在示例InnerEvenIterator内部类,它类似于标准的Java迭代器。所以我认为迭代器在Java中很常见?
It explains that in the example "The InnerEvenIterator inner class, which is similar to a standard Java iterator." So I take it that iterators are pretty common in Java?
我来自C编程背景。我不明白为什么这样的简单循环
I came from a C programming background. I don't understand why a simple loop like this
for(i=0;i <SIZE;i+2){
System.System.out.println(arrayOfInts[i]));
}
使用两种方法扩展到迭代器(内部类)。这里有什么意义?
got expanded to an iterators (inner class) with two methods. What's the point here?
public class DataStructure {
//create an array
private final static int SIZE = 15;
private int[] arrayOfInts = new int[SIZE];
public DataStructure() {
//fill the array with ascending integer values
for (int i = 0; i < SIZE; i++) {
arrayOfInts[i] = i;
}
}
public void printEven() {
//print out values of even indices of the array
InnerEvenIterator iterator = this.new InnerEvenIterator();
while (iterator.hasNext()) {
System.out.println(iterator.getNext() + " ");
}
}
//inner class implements the Iterator pattern
private class InnerEvenIterator {
//start stepping through the array from the beginning
private int next = 0;
public boolean hasNext() {
//check if a current element is the last in the array
return (next <= SIZE - 1);
}
public int getNext() {
//record a value of an even index of the array
int retValue = arrayOfInts[next];
//get the next even element
next += 2;
return retValue;
}
}
public static void main(String s[]) {
//fill the array with integer values and print out only values of even indices
DataStructure ds = new DataStructure();
ds.printEven();
}
}
推荐答案
对数组进行简单循环,你不会(通常)在Java中使用Iterator。
For simple loops over arrays, you would not (usually) use an Iterator in Java.
for(int i=0;i < arrayOfInts.length ; i+2){
System.out.println(arrayOfInts[i]));
}
迭代器的想法是解耦数据的存储方式(可能不会来自其使用者的代码(想要迭代它的代码)。
The idea of an iterator is to decouple how the data is stored (might not be an array) from its consumer (the code that wants to iterate over it).
你说Iterator是Java类库中一个非常核心的概念是正确的,从Java5开始就有这样的常见,就是支持它的for-each循环语言功能。使用此循环,用户甚至不再看到Iterator。
You are right to say that Iterator is a pretty core concept in the Java class library, so common that from Java5 on there is the for-each loop language feature to support it. With this loop, the user does not even see the Iterator anymore.
for(Something element: listOfSomething){
System.out.println(element);
}
如果我要实现偶数步进迭代器,我会以它为基础在普通迭代器上,因此它可以与任何类型的迭代一起使用。
If I were to implement an "even-stepping iterator" I would base it on the normal iterator, so that it can be used with any kind of iterable.
public class EvenSteppingIterator<X> implements Iterator<X>{
private final Iterator<X> source;
public EvenSteppingIterator(Iterator<X> source){
this.source = source;
// skip the first one, start from the second
if (source.hasNext()) source.next();
}
public boolean hasNext() {
return source.hasNext();
}
public X next(){
X n = source.next();
// skip the next one
if (source.hasNext()) source.next();
return n;
}
}
这篇关于为什么要写这样的迭代器呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!