List for iterator 迭代 效率 比較
public class Test {
public static void main(String[] args) {
List<Integer> firstList = new ArrayList<Integer>();
for(int i=0; i<10000; i++ ){
firstList.add(i);
}
long LongtimeStart = System.currentTimeMillis();
for(int i=0; i<firstList.size(); i++ ){
System.out.println(firstList.get(i));
}
long LongtimeStop = System.currentTimeMillis();
System.out.println(" for r ============== " + (LongtimeStop-LongtimeStart));
Iterator ite = firstList.iterator();
long LongtimeStart2 = System.currentTimeMillis();
while(ite.hasNext()){
System.out.println(ite.next());
}
long LongtimeStop2 = System.currentTimeMillis();
System.out.println(" iterator r ============== " + (LongtimeStop2-LongtimeStart2));
}
}
好像 iterator 的效率 要高一點點。。。時間不夠,,不確定。,。。
如果 數量多的話,就用 iterator ,如果數量 少的話,就用for ,,,不會太麻煩。、、