java Java中的迭代器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2889571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:18:21  来源:igfitidea点击:

Iterator in Java

javacollectionsiterator

提问by Kevin

What is Iterator and collections? Does these two have any relations?

什么是迭代器和集合?这两者有什么关系吗?

// the interface definition
Interface Iterator {
    boolean hasNext();
    Object next(); // note "one-way" traffic
    void remove();
}

// an example
public static void main (String[] args){
    ArrayList cars = new ArrayList();

    for (int i = 0; i < 12; i++)
        cars.add (new Car());

    Iterator it = cats.iterator();

    while (it.hasNext())
        System.out.println ((Car)it.next());
}

Does the Interface Iterator has these method names alone predefined or its user defined?. What does these four lines below actually tell?

接口迭代器是单独预定义这些方法名称还是它的用户定义?下面这四行实际上说明了什么?

cars.add (new Car());
Iterator it = cats.iterator();
while (it.hasNext())
    System.out.println ((Car)it.next());

Thanks. I am going through a book in collections.

谢谢。我正在浏览一本收藏的书。

回答by Roland Illig

The Java collections are, as the name says, collections of things. If you don't know that word, look it up in a dictionary.

顾名思义,Java 集合是事物的集合。如果你不认识这个词,就查字典。

There are many types of collections. Take for example the mathematical concept of a set. You can put arbitrary things in a set, but it will never contain the same thing more than once. The things in the set are not ordered, that is you cannot say A comes before B. Another type of collection is a list. A list can contain the same thing more than once, and the order of the things in the list is important.

有多种类型的集合。以集合的数学概念为例。你可以把任意的东西放在一个集合中,但它永远不会多次包含相同的东西。集合中的东西没有顺序,也就是说你不能说A 在 B 之前。另一种类型的集合是列表。一个列表可以多次包含相同的事物,列表中事物的顺序很重要。

What all these collections have in common is that they contain things, which in Java are called elements. When you want to know which things are in a certain collection, you iterate overthe collection, which is just another term for going throughall elements. This is what an Iteratordoes. It basically starts at the beginning of a collection, you can always ask whether there is a next element (hasNext()), and if there is, you can get access to that element (next()), until you have iterated over all elements in the collection.

所有这些集合的共同点是它们包含事物,在 Java 中称为元素。当您想知道某个集合中有哪些内容时,您可以遍历该集合,这只是遍历所有元素的另一个术语。这就是 an 的Iterator作用。它基本上从集合的开头开始,您可以随时询问是否有下一个元素 ( hasNext()),如果有,您可以访问该元素 ( next()),直到您遍历了集合中的所有元素。

回答by Foxfire

Technically iterators and collections are not directly related. However Iterators are mostly used together with collections to interate over the objects contained in the collection.

从技术上讲,迭代器和集合没有直接关系。然而,迭代器主要与集合一起使用,以对集合中包含的对象进行交互。

Iterators are a general and flexible concept that allows to interate objects without depending on the exact implementation of the entity that they iterate over.

迭代器是一个通用且灵活的概念,它允许在不依赖于它们迭代的实体的确切实现的情况下交互对象。

回答by Don Roby

An iterator is most commonly used as a mechanism for going through the elements of a collection.

迭代器最常用作遍历集合元素的机制。

The concept is not specific to Java at all, though the specific interface definition you show is.

尽管您展示的特定接口定义是特定于 Java 的,但该概念根本不是 Java 所特有的。

See the Wikipedia article on Iteratorfor some discussion of what it means and how it's done in assorted languages including Java.

请参阅有关 IteratorWikipedia 文章,了解它的含义以及它是如何在包括 Java 在内的各种语言中完成的。

In Java, it is an Interface, so you can indeed implement your own, but sensible ones are defined for the collections in Java's collections library and for any Java Collection implementation the method

在 Java 中,它是一个接口,因此您确实可以实现自己的接口,但是为 Java 集合库中的集合以及任何 Java 集合实现方法定义了合理的接口

collection.iterator()

should return an iterator that will traverse the elements of that collection.

应该返回一个迭代器,它将遍历该集合的元素。

Also see the javadoc for Collectionand Iteratorfor more.

另请参阅CollectionIterator的 javadoc了解更多信息。