list_arrayList三种遍历性能比较
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ListTest {
/**
* @param args
*/
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
for(int j=0;j<1000000;j++){
list.add(j);
}
long t1,t2;
t1=System.currentTimeMillis();
for(int i = 0;i<list.size();i++){
int a = list.get(i);
}
t2=System.currentTimeMillis();
System.out.println("Run1 Time:" + (t2 -t1) + "(ms)");
System.out.println("---------------------------");
t1=System.currentTimeMillis();
for(int listValue:list){
int b = listValue;
}
t2=System.currentTimeMillis();
System.out.println("Run2 Time:" + (t2 -t1) + "(ms)");
System.out.println("---------------------------");
t1=System.currentTimeMillis();
Iterator<Integer> iterato = list.iterator();
while(iterato.hasNext()){
int c = iterato.next();
}
t2=System.currentTimeMillis();
System.out.println("Run3 Time:" + (t2 -t1) + "(ms)");
}
}
result:Run1 Time:13(ms)
---------------------------
Run2 Time:34(ms)
---------------------------
Run3 Time:29(ms)
从结果可以看出第一种遍历速度最快