ArrayList Vs LinkedList

前端 未结 9 668
一整个雨季
一整个雨季 2020-11-27 10:53

I was following a previous post on this that says:

For LinkedList

  • get is O(n)
  • add is O(1)
  • remove is O(n)
9条回答
  •  情深已故
    2020-11-27 11:17

    An ArrayList is a simpler data structure than a LinkedList. An ArrayList has a single array of pointers in contiguous memory locations. It only has to be recreated if the array is expanded beyond its allocated size.

    A LinkedList consists of a chain of nodes; each node is separated allocated and has front and back pointers to other nodes.

    So what does this mean? Unless you need to insert in the middle, splice, delete in the middle etc. an ArrayList will usually be faster. It needs less memory allocations, has much better locality of reference (which is important for processor caching) etc.

提交回复
热议问题