LinkedList vs Array

In this post, we will learn the difference between LinkedList and Array in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.

Difference between LinkedList and Array in Java

LinkedList Array
A LinkedList is a dynamic data structure, which means it can grow or shrink during the execution of a program. An Array is a static data structure, which means its size is fixed at the time of creation.
LinkedList allows for the efficient insertion or removal of elements from any position, as it involves only updating the links. In an Array, the insertion or removal of elements in any position is costly because you have to shift elements.
LinkedList uses more memory than an array because it uses extra space for storing pointers to the next (and possibly previous) elements. An Array uses less memory because it only stores the elements.
LinkedList provides sequential access, i.e., to get to an element you have to traverse from the head node. This can take more time. An Array provides random access, i.e., you can access any element directly because they are indexed. This results in faster access times.
LinkedList doesn't need contiguous memory locations. Each node can be stored anywhere in memory. Arrays require contiguous memory locations. Elements of an array are stored together in memory.
In LinkedList, you don’t need to know the size at the time of creation. In Array, you must know the size at the time of creation.
In Java, the LinkedList class implements the List and Deque interfaces and can be used to create a singly or doubly linked list. In Java, Arrays are provided by native support and also by the java.util.Arrays class.
LinkedList does not support primitive types. Arrays support both primitive and reference types.
No direct support for operators like [ ] to access elements. Direct support for operators like [ ] to access elements.

References


Comments