why LinkedList doesn't have initialCapacity in java? why LinkedList doesn't have initialCapacity in java? arrays arrays

why LinkedList doesn't have initialCapacity in java?


LinkedList by nature does not have "capacity", since it does not allocate memory to the items before the items are added to the list. Each item in a LinkedList holds a pointer to the next in the list.

http://www.stoimen.com/blog/wp-content/uploads/2012/06/0.-Arrays-vs.-linked-list.png

There would be no point in allocating memory to the list beforehand, since LinkedList does not have capacity.


Its model is not based on an array but rather a true linked list, and so there is no need and further it would not make sense. It doesn't make much sense to have empty links like you have empty array items.


Why would you need a capacity on a LinkedList? A LinkedList does not work with fixed sized arrays. Every LinkedListElement has a pointer (a link!) to the next Element in the list. Which Because of that it is possible to add an element to a linked list in constant time. But it is costly to have random access to the elements in the List. You need to go through all the Elements in the list until you reach your destination.