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

时间:2009-12-16 05:32:50

标签: java

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

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

它解释了在示例“The 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();
    }
}

5 个答案:

答案 0 :(得分:6)

对于数组上的简单循环,你不会(通常)在Java中使用Iterator。

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

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

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

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

如果我要实现一个“偶数步进迭代器”,我会将它基于普通的迭代器,以便它可以与任何类型的迭代一起使用。

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;
      }


}

答案 1 :(得分:3)

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

接下来你会抱怨“你好世界”的节目没有做任何有用的事情!

答案 2 :(得分:3)

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

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

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

答案 3 :(得分:1)

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

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

BTW:欢迎使用Java!

干杯,

麦克

答案 4 :(得分:0)

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

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

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

相关问题