Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2561446
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-11-16 13:32:37

Iterator

An iterator is a way to iterate through a group of objects without having to worry about how to traverse that group. The goal is to not deal with the traversal implementation, but rather the abstract idea of "iteration", which is to view each item in the group exactly once.

迭代器:迭代器可以迭代一个集合或List,而不需要担心怎样遍历。它的目标是不处理遍历的实现,而是
怎样查看group中的每一项。

The pre-Iterator way to iterate is to use indexes:
以前迭代通过使用索引来实现:
  1. int myListMax = 3;
  2. for (int i = 0; i < myListMax; i++) {
  3. System.out.println(myList.get(i));
  4. }

This loop prints each item in myList to the console. But wait, does it set the last element in myList? Does myList contain 3 elements, or does it hold elements in the indexed positions 0,1,2,3 ? If the latter is true, this loop will fail to set the last element in myList.

These kind of "off-by-one" errors are subtle bugs that can be hard to track down. What if we had a more complicted data structure than a list that required more complicated traversal to get to each object--we'd have to figure that out each time.


Instead, we delegate iteration to the "container" (anything that holds a group of objects can be called a container) and use the Iterator interface to get the objects out of the container. Thus, we have a single interface for all containers, and we are guaranteed that if the container implements Iterator correctly, then we will see every single object in the container exactly once.

Using an iterator looks like this (suppose myList contains Strings):

迭代器的使用(假设myList中存放的是Strings):

  1. List myList = new ArrayList();
  2.  ...
  3. for (Iterator iter = myList.iterator() ; iter.hasNext() ; ) {
  4. String element = iter.next();
  5. System.out.println(element);
  6. }

In the init part of the for loop we created a variable called "iter". It is of type Iterator, which is what myList.iterator() returns. Iterator has two methods, hasNext() and next().
在for循环的初始化部分,我们创建了一个变量"iter",它的类型是Iterator,它用来保存
myList.iterator() 的返回值。Iterator有两个方法: hasNext() and next().

hasNext() returns true if there is another element in the iterator, and false if the iterator is empty (that is, if it has already shown you everything).next() returns the next element in the Iterator.
如果iterator里有另外的元素hasNext() 方法返回true;如果iterator中为空(如果你已经遍历出了其中所有元素)。next()返回iterator中的下一个元素,并且让iterator指向下一个元素。

Notice that the third part of the for () statement is empty. After the last ';' there is nothing but the closing ')' of the for statement. This is because calling iter.next() not only returns the next element, but also increments the iterator to point to the next element. You should only call iter.next() once per loop.
Iterators are so common in Java that there is syntactic sugar for dealing with them (introduced with version 1.5):


  1. for (String element : myList) {
  2.    System.out.println(element);
  3. }
does the exact same thing as the code above. You can only use this for classes that "implement" Iterable (and have a method called "iterator()"). Even if a class has a method that returns an iterator, you won't be able to use the syntactic sugar unless the class implements Iterable.


谢谢访问!

阅读(1774) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~