sqram sqram - 25 days ago 7
Java Question

How does LinkedList work internally in Java?

As far as I know, the concept of a linked list is a bunch of object connected to each other by having a 'next' and sometimes 'previous' attribute to traverse the objects.

I noticed in Java, you can create a LinkedList object...but treat it like an array/list/sequence by using the same methods such as .add(), .get(), etc.

So, is LinkedList internally an array-like sequence?

Answer Source

So, is LinkedList internally an array-like sequence?

No. It's a series of instances of a private nested class Entry, which has next, previous and element references. Note that you could have found this out yourself by looking at the source code, which comes with the JDK.

The reason why this internal structure is not exposed is that it prevents the strcture from becoming corrupted and e.g. containing a loop. And the unifrom access via the List and Deque interfaces allows polymorphic use.