I want to add an additional piece of information her about the difference in performance.
We already know that due to the fact that ArrayList
implementation is backed by an Object[]
it's supports random access and dynamic resizing and LinkedList
implementation uses references to head and tail to navigate it. It has no random access capabilities, but it supports dynamic resizing as well.
The first thing is that with an ArrayList, you can immediately access the index, whereas with a LinkedList, you have iterate down the object chain.
Secondly, inserting into ArrayList is generally slower because it has to grow once you hit its boundaries. It will have to create a new bigger array, and copy data from the original one.
But the interesting thing is that when you create an ArrayList that is already huge enough to fit all your inserts it will obviously not involve any array copying operations. Adding to it will be even faster than with LinkedList because LinkedList will have to deal with its pointers, while huge ArrayList just sets value at given index.
Check out for more ArrayList and LinkedList differences.