Iterator接口在Java中有什么好处? [英] What are the benefits of the Iterator interface in Java?

查看:504
本文介绍了Iterator接口在Java中有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚了解了Java Collections Framework如何在链表中实现数据结构。从我的理解,迭代器是一种遍历数据结构中的项目,如列表的方式。为什么使用此接口?为什么方法 hasNext() next() remove c $ c>不直接编码到数据结构实现本身?

I just learned about how the Java Collections Framework implements data structures in linked lists. From what I understand, Iterators are a way of traversing through the items in a data structure such as a list. Why is this interface used? Why are the methods hasNext(), next() and remove() not directly coded to the data structure implementation itself?

从Java网站: link text


public interface Iterator< E& p> An
迭代器。迭代器
代替了
Java集合框架中的枚举。迭代器
有两种方式不同于枚举:

public interface Iterator<E>

An iterator over a collection. Iterator takes the place of Enumeration in the Java collections framework. Iterators differ from enumerations in two ways:

  • 迭代器允许调用者从底层$ b $中删除
    元素b集合在迭代期间与
    良好定义的语义。
  • 方法名称
    已改进。

    • Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
    • Method names have been improved.

      我试着去搜索,似乎找不到一个明确的答案。有人可以说明为什么Sun选择使用它们吗?是因为更好的设计吗?增加安全性?良好的OO练习?

      I tried googling around and can't seem to find a definite answer. Can someone shed some light on why Sun chose to use them? Is it because of better design? Increased security? Good OO practice?

      任何帮助将非常感激。

      Any help will be greatly appreciated. Thanks.

      推荐答案


      为什么使用此介面?

      Why is this interface used?

      因为它支持基本操作,允许客户端程序员迭代任何类型的集合(注意:不一定是集合

      Because it supports the basic operations that would allow a client programmer to iterate over any kind of collection (note: not necessarily a Collection in the Object sense).


      为什么方法...不直接
      编码到数据结构
      实现本身?

      Why are the methods... not directly coded to the data structure implementation itself?

      所以你不能达到他们和与他们的粪。具体来说:

      They are, they're just marked Private so you can't reach into them and muck with them. More specifically:


      • 您可以实现或子类化 Iterator

      • 可以遍历的对象不需要使用遍历方法使它们的接口变得混乱,

      • 您可以向您希望的许多客户提供迭代器,每个客户都可以在他们的自己的时间,以自己的速度。

      • java.util包中的Java 迭代器当你还有一个 Iterator 时,它们被修改。此异常会让您知道 Iterator 现在可能返回无效对象。

      • You can implement or subclass an Iterator such that it does something the standard ones don't do, without having to alter the actual object it iterates over.
      • Objects that can be traversed over don't need to have their interfaces cluttered up with traversal methods, in particular any highly specialized methods.
      • You can hand out Iterators to however many clients you wish, and each client may traverse in their own time, at their own speed.
      • Java Iterators from the java.util package in particular will throw an exception if the storage that backs them is modified while you still have an Iterator out. This exception lets you know that the Iterator may now be returning invalid objects.

      对于简单的程序,这些都不值得。这种复杂性使它们很有用,但会很快出现在你身上。

      For simple programs, none of this probably seems worthwhile. The kind of complexity that makes them useful will come up on you quickly, though.

      这篇关于Iterator接口在Java中有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆