为什么要写这样的迭代器呢?

我正在阅读关于内部类的Java教程

http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html

它解释了在示例“InnerEvenIterator内部类,它类似于标准的Java迭代器”。 所以我认为迭代器在Java中很常见?

我来自C编程背景。 我不明白为什么这样的简单循环

for(i=0;i <SIZE;i+2){ System.System.out.println(arrayOfInts[i])); } 

用两种方法扩展到迭代器(内部类)。 这有什么意义?

 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(int i=0;i < arrayOfInts.length ; i+2){ System.out.println(arrayOfInts[i])); } 

迭代器的想法是从其使用者(想要迭代它的代码)中分离数据的存储方式(可能不是数组)。

你说Iterator是Java类库中一个非常核心的概念是正确的,因此从Java5开始就是通用的for-each循环语言function来支持它。 使用此循环,用户甚至不再看到Iterator。

 for(Something element: listOfSomething){ System.out.println(element); } 

如果我要实现一个“偶数步进迭代器”,我会将它建立在普通的迭代器上,这样它就可以用于任何类型的迭代。

 public class EvenSteppingIterator implements Iterator{ private final Iterator source; public EvenSteppingIterator(Iterator 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; } } 

这是内部类的说明,而不是最合适使用迭代器的示例。

接下来你会抱怨“hello world”程序没有做任何有用的事情!

迭代器是一种抽象。 抽象通常很好。

拥有迭代器的一件好事(大事吗?)能够以统一的方式检查不同的数据结构,即使数据结构不能用整数索引也是如此。 例如,在C ++中,您可以使用迭代器来运行数组,集合,向量,映射,甚至是您自己的数据结构,所有这些都以概念和语法统一的方式运行。 即使地图恰好是从Strings到Widgets!

使用C ++方法,使用迭代器始终至少与使用任何其他访问机制一样有效。 Java有不同的优先级。 Stephen是正确的,Java教程中的代码示例可能不是您想要拥有或使用迭代器的一个很好的例子。

我同意斯蒂芬C的观点,这是一个内部阶级的例证。 我想指出从C进入Java你可能会注意到更多面向对象的东西。 根据您对OO范例的熟悉程度,与您习惯的function编程风格相比,有些可能看起来很陌生。

内部类只是将itterator之类的东西转换为表示迭代器的对象的另一个例子。 通过这样做,我们获得了抽象层,这对面向对象的编程至关重要。

顺便说一句:欢迎来到Java!

干杯,

麦克风

我不认为我会在链接示例中概述的情况下使用内部类。 正如您所提到的,对于示例,带有for循环的C样式代码更好。 但我认为,由于本教程的目的是教育学习者内部类的使用,这个例子有助于理解内部类的概念,尽管它的实际用法看起来并不相关。

正如教程所提到的,Inner类在GUI编程中的事件处理部分非常有用。 对于事件处理,您可以将内部类的实例注册为EventHandler(对于GUI中的按钮上的操作),当对按钮执行操作时将调用其方法。 (单击按钮)(回调机制)。 内部类可以访问封闭类的私有成员,如果事件处理代码较小(在大多数情况下),则在GUI代码中使用内部类是有意义的。

在我看来,如果教程提供了一个内部类使用的示例,其中以GUI代码为例,它会让人感到困惑,并且不会达到目的。 我认为它应该是一个初学者的教程,并且经历它的人可能不熟悉使用Java进行GUI开发。