I learned that adding an element at the end of a LinkedList is O(1) since we're just adding linking an object to the last node of the list And ArrayList is also O(1), but worst case scenario is O(n) because of having to resize when there is no space in the array. I tried to put that to the test expecting the outcomes I learned, but the ArrayList was significantly faster in adding 10 million elements than the LinkedList.
After multiple tries, the ArrayList took around ~570 milliseconds, while the LinkedList took around ~1900 milliseconds.
public static void listPractice(List<String> test) {
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10_000_000; i++) {
test.add(String.valueOf(i));
}
System.out.println(System.currentTimeMillis() - startTime);
}
Seems very odd. I've been reading other discussions and I see a lot of programmers say that the LinkedList, in short words, kinda sucks. Is this evidence, or just something I'm missing?