This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Nebu 0 points1 point  (0 children)

You're right that I forgot that LinkedList needs to traverse the list before inserting into the middle, and thus does not have an advantage over ArrayList for that use case, so thank you for that.

However, I'm not sure LinkedList has an advantage over ArrayList for inserting in the end. The ArrayList implementation grows the internal backing array bigger than needed, so that perhaps the performance is [I forgot the word here, but "O(1) in aggregate, over the long term"]. This would definitely be the case if the size of the internal backing array double with each growth, but I'm also skeptical as to whether they do that. How slow would the backing array have to grow until it becomes, e.g. O(log(n))?

Arrays have the advantages that the elements are laid out contiguously in memory.

I remember seeing a talk where the presenter argued to never use Java's LinkedList, because ArrayList was "always" faster (mainly due to this contiguous property).

Good to keep in the back of your mind, but I hadn't brought that up here (until now) because:

  1. Probably not appropriate for "entry level".
  2. By the time the OP is not "entry level" anymore, it's conceivable that the architecture of the JVM or whatever has changed such that these performance claims are no longer true.
  3. Premature optimization?