我正在阅读何时在ArrayListArrayList
上使用 LinkedList?. 我开发了一个小示例应用程序来测试其主要优势,但我获得的结果并未证实,这超过了操作的性能:LinkedList
LinkedList
LinkedList
ArrayList
ListIterator.add(E element)
这是我的代码:
public static void main(String[] args) {
int number = 100000;
long startTime1 = System.currentTimeMillis();
fillLinkedList(number);
long stopTime1 = System.currentTimeMillis();
long startTime2 = System.currentTimeMillis();
fillArrayList(number);
long stopTime2 = System.currentTimeMillis();
System.out.println(" LinkedList needed: "+ (stopTime1 - startTime1));
System.out.println(" ArrayList needed: "+ (stopTime2 - startTime2));
}
public static void fillLinkedList(int number){
LinkedList<Integer> list = new LinkedList<Integer>();
ListIterator<Integer> it = list.listIterator();
int i = 0;
while(i++<number){
it.add(i);
}
// System.out.println("LinkedList size: "+list.size());
}
public static void fillArrayList(int number){
ArrayList<Integer> list = new ArrayList<Integer>();
ListIterator<Integer> it = list.listIterator();
int i = 0;
while(i++<number){
it.add(i);
}
// System.out.println("ArrayList size: "+list.size());
}
测量给出:
number 10,000 100,000 500,000 1,000,000 5,000,000
ArrayList 7 17 60 77 170
LinkedList 7 21 89 838 4127
我注意到元素的增加显着损害了LinkedList
while的性能,ArrayList
表现出相当好的行为。我理解错了吗?