I wonder why LinkedList
doesn\'t have initialCapacity
.
I know good when to use ArrayList
and when LinkedList
.
When you declare an array you have to know its size because pointers need to be created in memory. A linked list does not need this because there is no need for pointers to memory before any object is added to the list.
A linked list is defined recursively as: an empty list en element that points to the empty list
therefore whenever you add an element, you allocate memory (or rather in Java the compiler does this) when you create the element, and then when you add it to the list it now points to the list (or the last element in the list points to it).
So you don't need to declare initial size of linked list because a linked list always starts with the empty list, and when an element is added it points to the list.