Java FAQ: Top Questions
17. What is the difference between ArrayList and LinkedList?
ArrayList
and
LinkedList
are two implementations of the
List
interface, differing in performance and use cases.
-
ArrayList:
- Backed by a dynamic array.
-
Fast for random access (
get
,set
) due to indexing (O(1)). - Slow for insertions/deletions in the middle (O(n) due to shifting).
- Memory-efficient for static data.
-
LinkedList:
- Backed by a doubly-linked list.
- Fast for insertions/deletions (O(1) if position known).
- Slow for random access (O(n) due to traversal).
- Uses more memory due to node pointers.