ArrayList vs. LinkedList

ArrayList vs. LinkedList

3 mins read668 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on Oct 30, 2023 13:42 IST

ArrayList and LinkedList are linear data structure and are part of collection framework present in java.util packages. In this article, we will briefly discuss the difference between ArrayList and LinkedList in Java.

2022_09_ArrayList-vs-LinkedList.jpg

An array is a linearly ordered data structure in java used to store multiple elements (variables and objects of a class) of the same type in a contiguous memory location. But, once you have defined the size of the array, you can’t change it, i.e., the size of an array is predefined and fixed. 

So, now how to deal with this problem? 

We have ArrayList and LinkedList in Java that can be used or implemented to solve the above problem.

Must Read: Top Java Online Course and Certificates

Must Read: Free Java Online Course and Certificates

Table of Content

ArrayList

ArrayList is a part of the collection framework present in java.util package. It has a backing array and implements the list interface so that we can use all the methods of the list class.

  • ArrayList provides us with a dynamic array for storing the data of the same data type.
    • i.e., it can change its size when data is inserted or deleted
  • When 75% of the defined ArrayList is filled, a new and more significant memory block is allocated, and the previous values are copied to it.
  • ArrayList is slower than usual arrays in java.

Also Read: ArrayList in Java

Also Read: Implementing ArrayList in Java

Syntax

 
ArrayList <data_type> name = new ArrayList <data_type>();
Copy code

Adding New Data Points

We can add or insert data in ArrayList in two different ways:

  • add(object) – It will add the object at the end of the list
  • add(index, object) – It will add the object at the defined index location

Firstly, we will create an ArrayList of subjects:

 
ArrayList < String> Subject = new ArrayList ();
Subject.add(Math)
Subject.add(“Physics”)
Subject.add(“Chemistry”)
Subject.add(“Biology”)
Copy code

Output

Math Physics Chemistry Biology

Example

1st Method: Adding Subject at the end of the list

 
Subject.add(“English”);
Copy code

Output

Math Physics Chemistry Biology English

2nd Method: Adding Subject at the specified location

 
Subject.add(2, “English”);
Copy code

Output

Math Physics English Chemistry Biology

Modifying the Existing Data Points

We can modify any existing entry in the list using the ‘set’ function.

set(index, object) – The object is replaced with the new object at the specified index location.

Example: Replace English with Hindi in the above List

 
Subject.set(2, “Hindi”);
Copy code

Output

Math Physics Hindi Chemistry Biology

Delete Data Points from ArrayList

Data points in the ArrayList can be removed using two different methods using the Remove function:

Remove (object) – Function removes the defined object from the array list

  • If any data point is present multiple times in the ArrayList, then the first occurrence of the object is deleted

Remove (index, object) – The function removes the data point at the defined index location.

Must Check: Data Structures & Algorithms I: ArrayList, LinkedList, Stacks and Queues

LinkedList

LinkedList is a linear data structure and part of the collection framework similar to the ArrayList used to store and manipulate the data points. 

  • LinkedList is made of Nodes.
    • Each node contains object data and two pointers pointing last and next element (the address of the next and previous nodes).

There are two types of LinkedList

  • Singly LinkedList
    • Store the address of only the next node
    • Traverse the list in only one direction
  • Doubly Linked List: 
    • Stores the address of both next and previous nodes
    • Traverse the list in both directions.

Also Read: Introduction to Linked List Data Structure

Also Read: Singly Linked Lists

Syntax

 
LinkedList <data_type> name = new LinkedList <data_type>();
Copy code

Note: 

  • Adding, modifying and deleting data points in the LinkedList is similar to the adding, modifying, and deleting data points in the ArrayList.

ArrayList vs. LinkedList

ArrayList LinkedList
Uses a dynamic array to store the data. Uses doubly linked list to store data.
Insertion operation is slower than LinkedList.Time Complexity: O(N) Insertion operation is faster than the ArrayList.Time Complexity: O(1)
Manipulation with ArrayList is slower than LinkedList since it uses an array that requires the shifting of elements in memory. Manipulation with LinkedList is faster than ArrayList as it uses a doubly linked list that does not require shifting.
Used to store similar data type elements. Any type of data can be stored.
Memory is allocated at the compile time at the stack memory location. Memory is allocated during the run time at the heap memory section.
Contiguous memory is allocated to all the objects. Contiguous memory is not allocated.

Conclusion

In this article, we briefly discuss about ArrayList and LinkedList in Java and difference between ArrayList and LinkedList.

Hope, this article will help you to learn the difference between ArrayList and LinkedList.

Keep Learning!!

Keep Sharing!!

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio