public class program {
public static void main (String [] args) {
ArrayList Arraylist = new ArrayList();
long elements_to_fill = (long) 1E7;
long Current_milies = (long) System.currentTimeMillis();
for (int i = 0; i < elements_to_fill; i++) {
Arraylist.add(i);
}
System.out.println("Insertion in array list takes " + (System.currentTimeMillis()-Current_milies) + " ms");
LinkedList linkedlist = new LinkedList();
Current_milies = ( long ) System.currentTimeMillis();
for (int i = 0; i <elements_to_fill; i++) {
linkedlist.add(i);
}
System.out.println("insertion in linked list take " + (System.currentTimeMillis() - Current_milies) + " ms");
}
}
Output:
insertion in array list takes 3652 ms
insertion is Linked list take 6862 ms
The same with : long elements_to_fill = (long) 1E5;
Output:
insertion in array list takes 16 ms
insertion in linked list takes 8 ms
Insertion is array list take less time compared to linked list if we add 1E7 elements
But on the other hand if we add 1E7 elements than insertion in insertion is array list takes more time compared to linked list. Why this is happening?