I worked with LinkedList and ArrayList, and I know the concept of adding elements to ArrayList and LinkedList, but when I run the insert time check code, I get another insert time again and again for both LinkedList and ArrayList.
Sometimes, the LinkedList insertion time comes better, and vice versa, how exactly this happens, can someone tell me.
import java.util.ArrayList; public class Time { public static void main(String args[]) { int n=100000; long milis = System.currentTimeMillis(); ArrayList obj=new ArrayList(); for(int k=0;k<=n;k++) { obj.add(k); } System.out.println("insert arraylist takes " +(System.currentTimeMillis()-milis)+" ms"); } }
The output of this program
1) arraylist insert takes 13 ms 2) arraylist insert takes 9 ms
Second code
import java.util.LinkedList; public class Time1 { public static void main(String args[]) { int n=100000; long milis = System.currentTimeMillis(); LinkedList obj=new LinkedList(); for(int k=0;k<=n;k++) { obj.add(k); } System.out.println("insert linklist takes " +(System.currentTimeMillis()-milis)+" ms"); } }
Result of this
1) insert linklist takes 8 ms
2) insert linklist takes 17 ms
source share