一、Iterator是一个接口,强调松耦合。比如有一天list改成了set,你的for循环是不是就悲剧了~·
二、ListIterator看名字就知道是为ArrayList准备的迭代器,想想ArrayList的特点就清楚了。
三、关于速度的测试:
==================================
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
/**
* IteratorTest
* @author SageZk
*/
public class IteratorTest {
public static long testForloops(List<String> list) {
long start = 0L, end = 0L;
@SuppressWarnings("unused")
String le = null;
start = System.nanoTime();
for (int i = list.size() - 1; i >= 0; --i) {
le = list.get(i);
}
end = System.nanoTime();
return end - start;
}
public static long testIterator(List<String> list) {
long start = 0L, end = 0L;
@SuppressWarnings("unused")
String le = null;
start = System.nanoTime();
Iterator<String> it = list.iterator();
while (it.hasNext()) {
le = it.next();
}
end = System.nanoTime();
return end - start;
}
public static void main(String[] args) {
//测试列表长度
final int LEN = 10000;
//初始化测试用数据
List<String> arraylist = new ArrayList<String>();
List<String> linkedlist = new LinkedList<String>();
for (int i = 0; i < LEN; ++i) {
String s = Integer.toString(i, 2);
arraylist.add(s);
linkedlist.add(s);
}
//打印测试结果
final String FORMAT = "%1$-16s%2$-16s%3$16d\n";
System.out.println("List\t\tType\t\tTime(nanoseconds)");
System.out.println("-------------------------------------------------");
System.out.printf(FORMAT, "ArrayList", "for", testForloops(arraylist));
System.out.printf(FORMAT, "ArrayList", "Iterator", testIterator(arraylist));
System.out.printf(FORMAT, "LinkedList", "for", testForloops(linkedlist));
System.out.printf(FORMAT, "LinkedList", "Iterator", testIterator(linkedlist));
}
}
=============================
以下是 3 组测试结果:
[code=BatchFile]List Type Time(nanoseconds)
-------------------------------------------------
ArrayList for 1189258
ArrayList Iterator 2365594
LinkedList for 152396254
LinkedList Iterator 2340801
List Type Time(nanoseconds)
-------------------------------------------------
ArrayList for 1235701
ArrayList Iterator 4249982
LinkedList for 149825606
LinkedList Iterator 2525531
List Type Time(nanoseconds)
-------------------------------------------------
ArrayList for 1198267
ArrayList Iterator 2584617
LinkedList for 150382451
LinkedList Iterator 2347994
[/code]
for循环用于ArrayList的遍历还是可以的,但为了更大方面的考虑建议直接使用Iterator接口实现。另,LinkendList本身查询就慢,用for就更挂了
|