迭代器:
迭代器的工作是遍历并选择序列中的对象,而客户端程序员不必知道或关心该序列底层的结构。例如,Java的Iterator()(只能单向移动)可以用来:
- 使用方法Iterator()要求容器返回一个Iterator。Iterator将准备返回序列的第一个元素。
- 使用next()获得序列的下一个元素。
- 使用hasNext()检查序列中是否还有元素。
- 使用remove()将迭代器新近返回的元素删除。
先看下例:
//: holding/SimpleIteration.java
import typeinfo.pets.*;
import java.util.*;
public class SimpleIteration {
public static void main(String[] args) {
List<Pet> pets = Pets.arrayList(12);
Iterator<Pet> it = pets.iterator();
while(it.hasNext()) {
Pet p = it.next();
System.out.print(p.id() + ":" + p + " ");
}
System.out.println();
// A simpler approach, when possible:
for(Pet p : pets)
System.out.print(p.id() + ":" + p + " ");
System.out.println();
// An Iterator can also remove elements:
it = pets.iterator();
for(int i = 0; i < 6; i++) {
it.next();
it.remove();
}
System.out.println(pets);
}
} /* Output:
0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx 8:Cymric 9:Rat 10:EgyptianMau 11:Hamster
0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx 8:Cymric 9:Rat 10:EgyptianMau 11:Hamster
[Pug, Manx, Cymric, Rat, EgyptianMau, Hamster]
*///:~
再看下例,下例是一个display()方法,它不比知晓容器的确切类型:
//: holding/CrossContainerIteration.java
import typeinfo.pets.*;
import java.util.*;
public class CrossContainerIteration {
public static void display(Iterator<Pet> it) {
while(it.hasNext()) {
Pet p = it.next();
System.out.print(p.id() + ":" + p + " ");
}
System.out.println();
}
public static void main(String[] args) {
ArrayList<Pet> pets = Pets.arrayList(8);
LinkedList<Pet> petsLL = new LinkedList<Pet>(pets);
HashSet<Pet> petsHS = new HashSet<Pet>(pets);
TreeSet<Pet> petsTS = new TreeSet<Pet>(pets);
display(pets.iterator());
display(petsLL.iterator());
display(petsHS.iterator());
display(petsTS.iterator());
}
} /* Output:
0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx
0:Rat 1:Manx 2:Cymric 3:Mutt 4:Pug 5:Cymric 6:Pug 7:Manx
4:Pug 6:Pug 3:Mutt 1:Manx 5:Cymric 7:Manx 2:Cymric 0:Rat
5:Cymric 2:Cymric 7:Manx 1:Manx 3:Mutt 6:Pug 4:Pug 0:Rat
*///:~
请注意,display()方法不包含任何有关它所遍历的序列的类型信息,而这也展示了Iterator的真正威力:能够将遍历序列的操作与序列底层的结构分离。
ListIterator是一个更加强大的Iterator得子类型,它只能用于各种List类的访问,但是ListIterator可以双向移动。
总结:
- 数组保存类型明确的对象。它可以是多维的,可以保存基本类型的数据。但是一旦生成,容量就不能改变。
- Collection保存单一元素。Map保存相关联的键值对。二者都可以在你向其中添加更多的元素时,自动调整尺寸。容器不能持有基本类型,但是自动包装机制会仔细的执行基本类型到容器中所持有的包装器类型之间的双向转换。
- 数组和List都是排好序的容器。List能够自动扩充容量。
- 如果要进行大量的随机访问,就用ArrayList;如果经常从表中间插入或删除元素,则应该使用LinkedList。
- 各种Queue以及栈 的行为,都是由LinkedList提供支持的。
- Map是一种将对象与对象相关联的设计。HashMap设计用来快速访问;而TreeMap保持“键”始终处于排序状态,所以没有HashMap快;LinkedHashMap保持元素插入的顺序,但是也通过散列提供了快速访问的能力。
- Set不接受重复元素。HashSet提供最快的查询速度。而TreeSet保持元素处于排序状态。LinkedHashSet以插入顺序保存元素。
- 新程序中不应该使用过时的Vector,HashTable和Stack。