LinkedList.get() vs ArrayList.get()
The most significant difference between Array and LinkedList is that array stores its element at the contiguous location while linked list stores its data anywhere in memory.
- LinkedList.get(index) returns you the object in the list
- ArrayList.get(index) also returns the object in the list
- Both of them has index
Question – If both has index, then why do we say that getting elements from arraylist is FASTer and getting elements from Linkedlist is slower ..?
So the difference is
- what does ‘index’ refers to
- what is the internal implementation of get() in both of these
- In ArrayList, index refers to the actual object in memory.
There is an underlying array. The index holds the index of the array which contains the values.
- In LinkedList the index refers to the position of the object, and it doesn’t point to actual object.
LinkedList has no array .