Difference between ArrayList, LinkedList and Vector
ArrayList:
Array List is an implemented class of List interface which is present in package java.util. Array List is created on the basis of the growable or resizable array. And Array List is an index-based data structure. In ArrayList, the element is stored in a contiguous location. It can store different data types. And random access is allowed. We can also store the duplicate element in Array List. It can store any number of null values.
Below is the implementation of ArrayList:
Java
// Java program to Illustrate working of an ArrayList // Importing required classes import java.io.*; import java.util.*; // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating an ArrayList of Integer type ArrayList<Integer> arrli = new ArrayList<Integer>(); // Appending the new elements // at the end of the list // using add () method via for loops for ( int i = 1 ; i <= 5 ; i++) arrli.add(i); // Printing the ArrayList System.out.println(arrli); // Removing an element at index 3 // from the ArrayList // using remove() method arrli.remove( 3 ); // Printing the ArrayList after // removing the element System.out.println(arrli); } } |
[1, 2, 3, 4, 5] [1, 2, 3, 5]
Linked List:
Linked list is a linear data structure where data are not stored sequentially inside the computer memory but they are link with each other by the address. The best choice of linked list is deletion and insertion and worst choice is retrieval . In Linked list random access is not allowed . It traverse through iterator.
Below is the implementation of the LinkedList:
C++
#include <iostream> // LinkedList class definition class LinkedList { public : // Node structure definition within LinkedList class struct Node { int data; Node *next; // Node constructor Node( int d) : data(d), next(nullptr) {} }; // Pointer to head node Node *head; // Constructor LinkedList() : head(nullptr) {} // Function to print the linked list void printList() { // Pointer to traverse the linked list Node *n = head; while (n != nullptr) { // Print the data of the node std::cout << n->data << " " ; // Move to the next node n = n->next; } } }; // Main function int main() { // Create an instance of the LinkedList class LinkedList llist; // Create three nodes with data 1, 2 and 3 llist.head = new LinkedList::Node(1); LinkedList::Node *second = new LinkedList::Node(2); LinkedList::Node *third = new LinkedList::Node(3); // Connect the first node with the second node llist.head->next = second; // Connect the second node with the third node second->next = third; // Call the printList function to print the linked list llist.printList(); return 0; } |
Java
import java.util.*; // LinkedList class definition class LinkedList { // Node class definition within LinkedList class static class Node { int data; Node next; // Node constructor Node( int d) { this .data = d; next = null ; } } // Pointer to head node Node head; // Constructor LinkedList() { head = null ; } // Function to print the linked list void printList() { // Pointer to traverse the linked list Node n = head; while (n != null ) { // Print the data of the node System.out.print(n.data + " " ); // Move to the next node n = n.next; } } } // Main class public class Main { public static void main(String[] args) { // Create an instance of the LinkedList class LinkedList llist = new LinkedList(); // Create three nodes with data 1, 2 and 3 llist.head = new LinkedList.Node( 1 ); LinkedList.Node second = new LinkedList.Node( 2 ); LinkedList.Node third = new LinkedList.Node( 3 ); // Connect the first node with the second node llist.head.next = second; // Connect the second node with the third node second.next = third; // Call the printList function to print the linked list llist.printList(); //This code is Contributed by Abhijit Ghosh } } |
C#
// C# program to define a LinkedList class using System; // LinkedList class definition class LinkedList { // Node structure definition within LinkedList class public class Node { public int data; public Node next; // Node constructor public Node( int d) { data = d; next = null ; } } // Pointer to head node public Node head; // Constructor public LinkedList() { head = null ; } // Function to print the linked list public void PrintList() { // Pointer to traverse the linked list Node n = head; while (n != null ) { // Print the data of the node Console.Write(n.data + " " ); // Move to the next node n = n.next; } } } // Main function class GFG { static void Main() { // Create an instance of the LinkedList class LinkedList llist = new LinkedList(); // Create three nodes with data 1, 2 and 3 llist.head = new LinkedList.Node(1); LinkedList.Node second = new LinkedList.Node(2); LinkedList.Node third = new LinkedList.Node(3); // Connect the first node with the second node llist.head.next = second; // Connect the second node with the third node second.next = third; // Call the PrintList function to print the linked list llist.PrintList(); } } |
1 2 3
Vector:
The Vector class implements a growable array of objects. Vectors fall in legacy classes, but now it is fully compatible with collections. It is found in java.util package and implement the List interface
Below is the implementation of the Vector:
C++
#include <iostream> #include<vector> using namespace std; int main() { // Size of the Vector int n = 5; // Declaring the Vector with // initial size n vector< int > v; // Appending new elements at // the end of the vector for ( int i = 1; i <= n; i++) v.push_back(i); // Printing elements for ( auto i : v) cout<<i<< " " ; cout<<endl; // Remove element at index 3 v.erase(v.begin()+3); // Displaying the vector // after deletion for ( auto i : v) cout<<i<< " " ; cout<<endl; return 0; } |
Java
// Java Program to Demonstrate Working // of Vector Via Creating and using it // Importing required classes import java.io.*; import java.util.*; // Main class class GFG { // Main driver method public static void main(String[] args) { // Size of the Vector int n = 5 ; // Declaring the Vector with // initial size n Vector<Integer> v = new Vector<Integer>(n); // Appending new elements at // the end of the vector for ( int i = 1 ; i <= n; i++) v.add(i); // Printing elements System.out.println(v); // Remove element at index 3 v.remove( 3 ); // Displaying the vector // after deletion System.out.println(v); // iterating over vector elements // using for loop for ( int i = 0 ; i < v.size(); i++) // Printing elements one by one System.out.print(v.get(i) + " " ); } } |
[1, 2, 3, 4, 5] [1, 2, 3, 5] 1 2 3 5
Difference between Array List, Linked List, and Vector:
Subject | Array List | Linked List | Vector |
---|---|---|---|
synchronized | Not present | Not present | present |
Random access | Allowed | Not Allowed | Allowed |
Memory Location | contiguous | Not contiguous | contiguous |
Null values | supports | supports | supports |
Data structure | Dynamic Array | Doubly Linked List | Dynamic Array |
Duplicate allowed | Yes | Yes | Yes |
Operation | Insertion and deletion are slow | Insertion and deletion are fast | Insertion and deletion are slow |
Which one is better among Linked list, Array list, or Vector?
It depends on the specific use case, each of these data structures has its own advantages and trade-offs. If you mostly need to insert and delete elements at the start or middle of the container, then a linked list might be a better option. If you need fast random access and are willing to accept slower insertion and deletion at end positions, an Array List or Vector is a better option.
Please Login to comment...