7 Difference Between Linked List and Array in Java with Examples

Difference between Linked List and Array in java is a popular interview question. This question is important for the interviewer because it is generic i.e this question is valid for C programmers, C++  programmers and Python programmers. This question is easy to answer. You can find this question in the initial rounds of the interview. Make sure you go through this question and build knowledge around the popular data structures Array and Linked List. Difference between Linked List and Array is different from the another popular core java interview question difference between ArrayList and LinkedList  in java.

Read Also : Difference between Array and ArrayList in Java

Difference between Linked List and Array in Java

1. Size :  Array is static in size. You need to define the size of Array at the time of declaration. The size of the Array once defined can not be changed.

Size of Linked List is dynamic in nature. You can add as many elements as you want to the Linked List unless memory becomes a constraint.

2. Memory Used : For storing the same number of elements, Array requires less memory than Linked List. Array only stores data(element) where as Linked List stores data (element) as well as the next node address.

3. Memory Allocation : Another difference between Linked List and Array is memory allocation.

An Array requires contiguous memory allocation, where as Linked List elements are present all over the heap memory. There is no limitation of contiguous memory in the case of Linked List.

difference between Linked List and Array in Java

4. Performance : Performance helps us to understand when to use Array over Linked List or when to use Linked List over Array.

Linked List gives O(n) performance for searching an element while Array gives O(1) performance for searching an element given you know the index.

Linked List is best suited for insertion and deletion operations. If you insert or delete the element at head or tail then the performance is O(1), otherwise the performance is O(n) , whereas Array gives O(n) performance for inserting or deleting an element.

5. Multi-Dimensional : The another difference between Linked List and Array is dimension. Array can be multi-dimensional in java.
Due to multi-dimensional  nature of Array, it can be useful to represent matrices, 2d plain , 2d game etc.

Linked List is always single-dimensional but it is of two types, single linked list and double linked list.

6. Length : Length of the Linked List can be provided by the size() method where as Array has the  length variable which returns the length of the Array. 

for example :

Integer arrayObject[] = new Integer[10];
arraylength= arrayObject.length   ;  //uses arrayobject length variable


LinkedList  linkedlistObject = new LinkedList();
linkedlistObject.add(12);
linkedlistObject.size();   //uses linkedlistObject size method

7. Adding elements : You can insert the elements into the Linked List object using add() method where as Array uses assignment operator(=) to insert the elements.

Integer addarrayobject[] = new Integer[3];
addarrayobject[0]= new Integer(8)   ;  //new object is added to the array object

Example of Linked List and Array


import java.util.LinkedList;
import java.util.Iterator;

public class ArrayLinkedListExample {
    
    public static void main(String[] args) { 

        // LinkedList Example   

        LinkedList<String> linkedlistobj = new LinkedList<String>();
        linkedlistobj.add("Alive is awesome");
        linkedlistobj.add("Love yourself");

        Iterator it = linkedlistobj.iterator();

        System.out.println("Linked List object output :");  

        while(it.hasNext())         

          System.out.print(it.next() + " ");


        // Array Example

        String[] arrayobj = new String[3];
        arrayobj[0]= "Love yourself";  
        arrayobj[1]= "Alive is awesome";

        arrayobj[2]= "Be in Present"; 
        
        System.out.println("");
        System.out.println("Array object output :");

        for(int i=0; i < arrayobj.length ;i++)

        System.out.print(arrayobj[i] + " ");   

 }
}

Output :
Linked List object output :
Alive is awesome Love yourself
Array object output :
Love yourself Alive is awesome Be in Present

Similarities between Linked List and Array in Java

1. Data structures : Linked List and Array both are data structures.

2. Duplicate elements : Both Linked List and Array can contain duplicate elements.

3. Null values : Both Linked List and Array can store null values and use index to refer to the elements.

Recap : Difference between Linked List and Array in Java


Linked ListArray
SizeDynamic Fixed (Static)
Memory UsedRequires more memoryRequires less memory
Memory AllocationElements scattered around heap memoryContiguous memory allocation
PerformanceO(1) for insertion, deletionO(1) for searching
Multi-DimensionalNoYes
LengthUses size() methodUses length variable
Adding ElementsUses add() methodUses assignment operator

That's all for difference between Linked List and Array in java. If you have any questions or doubts then please mention in the comments.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry