When I run (respectively):
package containers;
import java.util.*;
public static void main(String[] args) {
List<Integer> arLst = new ArrayList<Integer>();
List<Integer> lnLst = new LinkedList<Integer>();
long start = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
arLst.add(i);
}
System.out.println("Array list: "+Long.toString(System.currentTimeMillis()-start));
start = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
lnLst.add(i);
}
System.out.println("Linked list: "+Long.toString(System.currentTimeMillis()-start));
}
I get roughly the same executing time. I know that Adding time should be faster for LinkedList. I wonder why.. (It makes sense that both for middle insertinon and last elemnt - since arrays know in O(1) where to insert, unlike LinkedList that has to go through the whole list, as I recall).