// 向list的位置0插入COUNT个数 for (inti=0; i<COUNT; i++) { list.add(0, i); }
longendTime= System.currentTimeMillis(); longinterval= endTime - startTime; System.out.println(getListName(list) + " : insert "+COUNT+" elements into the 1st position use time:" + interval+" ms"); }
// 删除list第一个位置元素 for (inti=0; i<COUNT; i++) { list.remove(0); }
longendTime= System.currentTimeMillis(); longinterval= endTime - startTime; System.out.println(getListName(list) + " : delete "+COUNT+" elements from the 1st position use time:" + interval+" ms"); }
// 读取list元素 for (inti=0; i<COUNT; i++) { list.get(i); }
longendTime= System.currentTimeMillis(); longinterval= endTime - startTime; System.out.println(getListName(list) + " : read "+COUNT+" elements by position use time:" + interval+" ms"); } }
运行结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Stack : insert 100000 elements into the 1st position use time:1060 ms Vector : insert 100000 elements into the 1st position use time:981 ms LinkedList : insert 100000 elements into the 1st position use time:8 ms ArrayList : insert 100000 elements into the 1st position use time:986 ms
Stack : read 100000 elements by position use time:3 ms Vector : read 100000 elements by position use time:0 ms LinkedList : read 100000 elements by position use time:3583 ms ArrayList : read 100000 elements by position use time:1 ms
Stack : delete 100000 elements from the 1st position use time:926 ms Vector : delete 100000 elements from the 1st position use time:928 ms LinkedList : delete 100000 elements from the 1st position use time:5 ms ArrayList : delete 100000 elements from the 1st position use time:909 ms