why LinkedList doesn't have initialCapacity in java?

前端 未结 6 1023
不知归路
不知归路 2021-02-07 00:54

I wonder why LinkedList doesn\'t have initialCapacity.

I know good when to use ArrayList and when LinkedList.

6条回答
  •  广开言路
    2021-02-07 01:35

    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.

提交回复
热议问题