Java 中的迭代器
什么是迭代器和集合? 这两者有什么关系吗?
// 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());
}
接口迭代器是否单独预定义了这些方法名称,还是由用户定义的? 下面这四行实际上说明了什么?
cars.add (new Car());
Iterator it = cats.iterator();
while (it.hasNext())
System.out.println ((Car)it.next());
谢谢。我正在浏览一本收藏书。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Java 集合,顾名思义,是事物的集合。如果你不认识这个词,就查字典。
收藏有很多种类型。以集合的数学概念为例。您可以将任意事物放入一个集合中,但它永远不会多次包含相同的事物。集合中的事物没有顺序,也就是说你不能说A在B之前。另一种类型的集合是列表。列表可以多次包含相同的内容,并且列表中内容的顺序很重要。
所有这些集合的共同点是它们都包含一些东西,这些东西在 Java 中被称为元素。当您想知道某个集合中有哪些内容时,您可以迭代该集合,这只是遍历所有元素的另一个术语。这就是
Iterator
的作用。它基本上从集合的开头开始,您可以随时询问是否有下一个元素(hasNext()
),如果有,您可以访问该元素(next ()
),直到迭代完集合中的所有元素。The Java collections are, as the name says, collections of things. If you don't know that word, look it up in a dictionary.
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.
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 over the collection, which is just another term for going through all elements. This is what an
Iterator
does. 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.从技术上讲,迭代器和集合没有直接关系。然而,迭代器主要与集合一起使用来对集合中包含的对象进行交互。
迭代器是一个通用且灵活的概念,允许交互对象而不依赖于它们迭代的实体的确切实现。
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.
迭代器最常用作遍历集合元素的机制。
尽管您显示的具体接口定义是特定于 Java 的,但该概念根本不是特定于 Java 的。
请参阅关于 Iterator 的维基百科文章,了解有关 Iterator 的含义以及如何在各种语言中完成它的一些讨论包括Java。
在 Java 中,它是一个接口,因此您确实可以实现自己的接口,但合理的接口是为 Java 集合库中的集合定义的,并且对于任何 Java 集合实现,该方法
应该返回一个将遍历该集合的元素的迭代器。
另请参阅 Collection 和 < a href="http://java.sun.com/javase/6/docs/api/java/util/Iterator.html" rel="nofollow noreferrer">迭代器 了解更多信息。
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.
See the Wikipedia article on Iterator for some discussion of what it means and how it's done in assorted languages including 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
should return an iterator that will traverse the elements of that collection.
Also see the javadoc for Collection and Iterator for more.