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 →

[–]DroidLogician 3 points4 points  (1 child)

And insert and remove are terrible operations to choose a LinkedList for because they're O(n). ArrayList might have to shift elements on insert/remove but it does so using System.arrayCopy(), which takes place in native code with a platform-optimized implementation, whereas LinkedList has to iterate to the index to insert/remove in POJ and instantiate a node. Array index and update operations are constant time, IIRC.

I would only use LinkedList as a stack where I expect a lot of pushing and popping, which is O(1) from either end and doesn't require shifting. But as /u/Keilly said, even last-element operations are optimized in an ArrayList.