日期:2014-05-20 浏览次数:20964 次
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
Entry<E> e = header;
if (index < (size >> 1)) {
for (int i = 0; i <= index; i++)
e = e.next;
} else {
for (int i = size; i > index; i--)
e = e.previous;
}
return e;
------解决方案--------------------
楼主,我用100000最小数据测试了一下:
import java.util.*;
public class CMPfor
{
final long N = 1000000;
private ArrayList<Integer> aT = new ArrayList<Integer>();
private LinkedList<Integer> LT = new LinkedList<Integer>();
public CMPfor(){
for(int i = 0; i < N; i++){
aT.add(i);
}
for(int i = 0; i < N; i++){
LT.add(i);
}
}
public void forTest(List<Integer> t){
for(int i = 0; i < t.size();i++){
t.get(i);
}
}
public void foreachTest(List<Integer> t){
for(int j : t){
}
}
public void iteratorTest(List<Integer> t){
Iterator it = t.iterator();
while(it.hasNext()){
it.next();
}
}
public void whileTest(List<Integer> t){
int i = 0;
while(i < N){
t.get(i);
i++;
}
}
public static void main(String[] args){
CMPfor cf = new CMPfor();
long a,b;
System.out.println("ArrayList Test:");
a=System.currentTimeMillis();
cf.forTest(cf.aT);
b=System.currentTimeMillis();
System.out.println("forTest: "+(b-a)+"ms");
a=System.currentTimeMillis();
cf.foreachTest(cf.aT);
b=System.currentTimeMillis();
System.out.println("foreachTest: "+(b-a)+"ms");
a=System.currentTimeMillis();
cf.iteratorTest(cf.aT);
b=System.currentTimeMillis();
System.out.println("iteratorTest: "+(b-a)+"ms");
a=System.currentTimeMillis();
cf.whileTest(cf.aT);
b=System.currentTimeMillis();
System.out.println("whileTest: "+(b-a)+"ms");
System.out.println("LinkedList Test:");
a=System.currentTimeMillis();
cf.forTest(cf.LT);
b=System.currentTimeMillis();
System.out.println("forTest: "+(b-a)+"ms");
a=System.currentTimeMillis();
cf.foreachTest(cf.LT);
b=System.currentTimeMillis();
System.out.println("foreachTest: "+(b-a)+"ms");
a=System.currentTimeMillis();
cf.iteratorTest(cf.LT);
b=System.currentTimeMillis();
System.out.println("iteratorTest: "+(b-a)+"ms");
a=System.currentTimeMillis();
cf.whileTest(cf.LT);
b=System.currentTimeMillis();
System.out.println("whileTest: "+(b-a)+"ms");
}
}