比較一:添加內容 涉及方法:add 註:這是ArrayList的測試代碼,但是測LinkedList只要把addlist的類型改一下就行,改成LinkedList 結果: ArrayList LinkedList 顯然在插入10000000條數據時,耗時LinkedList>ArrayList 比較 ...
比較一:添加內容
涉及方法:add
public void add_test(){ List<Person> addlist = new ArrayList<Person>(); Person p = new Person("阿呆",20);
long begin = System.currentTimeMillis(); System.out.println("ArrayList 開始時間:"+begin); for(int i=0; i<10000000; i++){ addlist.add(p); // System.out.println("第"+i+"個"+"所用時間:"+(System.currentTimeMillis()-begin)); } long over = System.currentTimeMillis(); System.out.println("ArrayList 結束時間:"+over); System.out.println("ArrayList 最終時間:"+(over-begin)); }
註:這是ArrayList的測試代碼,但是測LinkedList只要把addlist的類型改一下就行,改成LinkedList
結果:
ArrayList
LinkedList
顯然在插入10000000條數據時,耗時ArrayList < LinkedList
比較二:獲取內容
涉及方法:get、contains、indexOf、lastIndexOf
1、get測試
public void get_test(){ //準備工作,添加一堆阿呆,添加一個阿傻 List<Person> getlist = new ArrayList<Person>(); Person p = new Person("阿呆",20); for(int i=0; i<10000000; i++){ getlist.add(p); } Person p2 = new Person("阿傻",20); getlist.add(77777,p2); System.out.println("添加完成"); // get 測試 long begin = System.currentTimeMillis(); System.out.println("get測試開始時間:"+begin); getlist.get(7777777); long over = System.currentTimeMillis(); System.out.println("get測試結束時間:"+over); System.out.println("get測試最終時間:"+(over-begin)); }
結果
ArrayList
LinkedList
2、contains測試
// contains 測試 long begin = System.currentTimeMillis(); for(int i=0; i<10000; i++){ getlist.contains(p2); } long over = System.currentTimeMillis(); System.out.println("contains測試最終時間:"+(over-begin));
註:只要把上面測試add的那段代碼換成這段,就可以測試contains,準備工作的代碼不變
結果
ArrayList
LinkedList
3、indexOf測試
// indexOf 測試 long begin = System.currentTimeMillis(); for(int i=0; i<10000; i++){ getlist.indexOf(p2); } long over = System.currentTimeMillis(); System.out.println("indexOf測試最終時間:"+(over-begin));
結果
ArrayList
LinkedList
4、lastIndexOf測試
// lastIndexOf 測試 long begin = System.currentTimeMillis(); for(int i=0; i<100; i++){ getlist.lastIndexOf(p2); } long over = System.currentTimeMillis(); System.out.println("lastIndexOf 測試最終時間:"+(over-begin));
結果
ArrayList
LinkedList
小結:
get:ArrayList < LinkedList,差別很大
contains:ArrayList < LinkedList,差別不那麼大,以下兩個也是
indexOf:ArrayList < LinkedList
lastIndexOf:ArrayList < LinkedList
比較三:刪除內容
涉及方法:remove(根據索引)、remove(根據對象)
1、remove(根據索引)
public void remove_test(){ //準備工作,添加一堆阿呆 List<Person> getlist = new ArrayList<Person>(); Person p = new Person("阿呆",20); for(int i=0; i<10000000; i++){ getlist.add(p); } System.out.println("添加完成"); // remove(int index) 測試 System.out.println(getlist.size()); long begin = System.currentTimeMillis(); for(int i=0; i<10000000; i++){ getlist.remove(getlist.size()-1); } long over = System.currentTimeMillis(); System.out.println("remove(int index) 測試最終時間:"+(over-begin)); System.out.println(getlist.size());
}
結果
ArrayList
LinkedList
2、remove(根據對象)
// remove(Object o) 測試 System.out.println(getlist.size()); long begin = System.currentTimeMillis(); for(int i=0; i<999; i++){ getlist.remove(p); } long over = System.currentTimeMillis(); System.out.println("remove(Object o) 測試最終時間:"+(over-begin)); System.out.println(getlist.size());
結果
ArrayList
LinkedList
小結:
remove(根據索引):ArrayList < LinkedList,差別不大