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:
C++
#include <iostream>
#include <vector>
usingnamespacestd;
intmain()
{
// Creating a vector of int type
vector<int> vec;
// Appending new elements
// at the end of the vector
// using push_back() method via for loops
for(inti = 1; i <= 5; i++)
vec.push_back(i);
// Printing the vector
for(inti = 0; i < vec.size(); i++)
cout << vec[i] << " ";
cout << endl;
// Removing an element at index 3
// from the vector
// using erase() method
vec.erase(vec.begin() + 3);
// Printing the vector after
// removing the element
for(inti = 0; i < vec.size(); i++)
cout << vec[i] << " ";
cout << endl;
return0;
}
// This code is contributed by Akash Jha
Java
// Java program to Illustrate working of an ArrayList
# Appending new elements to the list using for loop
fori inrange(1, 6):
my_list.append(i)
# Printing the list
print(my_list)
# Removing an element at index 3 from the list
my_list.pop(3)
# Printing the list after removing the element
print(my_list)
C#
// C# program to Illustrate working of an ArrayList
// Importing required namespaces
usingSystem;
usingSystem.Collections;
// Main class
classGFG {
// Main driver method
staticvoidMain(string[] args)
{
// Creating an ArrayList of integer type
ArrayList arrli = newArrayList();
// Appending the new elements
// at the end of the list
// using Add() method via for loops
for(inti = 1; i <= 5; i++)
arrli.Add(i);
// Printing the ArrayList
foreach(inti inarrli) Console.Write(i + " ");
Console.WriteLine();
// Removing an element at index 3
// from the ArrayList
// using RemoveAt() method
arrli.RemoveAt(3);
// Printing the ArrayList after
// removing the element
foreach(inti inarrli) Console.Write(i + " ");
Console.WriteLine();
}
}
// This code is contributed by Akash Jha
Javascript
let vec = [];
// Appending new elements
// at the end of the vector
// using push() method via for loops
for(let i = 1; i <= 5; i++) {
vec.push(i);
}
// Printing the vector
for(let i = 0; i < vec.length; i++) {
console.log(vec[i] + " ");
}
console.log("\n");
// Removing an element at index 3
// from the vector
// using splice() method
vec.splice(3, 1);
// Printing the vector after
// removing the element
for(let i = 0; i < vec.length; i++) {
console.log(vec[i] + " ");
}
console.log("\n");
//This code is contributed by Akash Jha
Output
[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
classLinkedList {
public:
// Node structure definition within LinkedList class
// Call the printList function to print the linked list
llist.printList();
return0;
}
Java
importjava.util.*;
// LinkedList class definition
classLinkedList {
// Node class definition within LinkedList class
staticclassNode {
intdata;
Node next;
// Node constructor
Node(intd) {
this.data = d;
next = null;
}
}
// Pointer to head node
Node head;
// Constructor
LinkedList() {
head = null;
}
// Function to print the linked list
voidprintList() {
// 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
publicclassMain {
publicstaticvoidmain(String[] args) {
// Create an instance of the LinkedList class
LinkedList llist = newLinkedList();
// Create three nodes with data 1, 2 and 3
llist.head = newLinkedList.Node(1);
LinkedList.Node second = newLinkedList.Node(2);
LinkedList.Node third = newLinkedList.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
}
}
Python3
classLinkedList:
# Node structure definition within LinkedList class
classNode:
def__init__(self, data):
self.data =data
self.next=None
def__init__(self):
# Pointer to head node
self.head =None
defprintList(self):
# Pointer to traverse the linked list
n =self.head
whilen isnotNone:
# Print the data of the node
print(n.data, end=' ')
# Move to the next node
n =n.next
if__name__ =='__main__':
# Create an instance of the LinkedList class
llist =LinkedList()
# Create three nodes with data 1, 2 and 3
llist.head =LinkedList.Node(1)
second =LinkedList.Node(2)
third =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()
C#
// C# program to define a LinkedList class
usingSystem;
// LinkedList class definition
classLinkedList {
// Node structure definition within LinkedList class
publicclassNode {
publicintdata;
publicNode next;
// Node constructor
publicNode(intd) {
data = d;
next = null;
}
}
// Pointer to head node
publicNode head;
// Constructor
publicLinkedList() {
head = null;
}
// Function to print the linked list
publicvoidPrintList() {
// 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
classGFG {
staticvoidMain() {
// Create an instance of the LinkedList class
LinkedList llist = newLinkedList();
// Create three nodes with data 1, 2 and 3
llist.head = newLinkedList.Node(1);
LinkedList.Node second = newLinkedList.Node(2);
LinkedList.Node third = newLinkedList.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();
}
}
Javascript
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
printList() {
let n = this.head;
while(n != null) {
console.log(n.data + " ");
n = n.next;
}
}
}
let llist = newLinkedList();
llist.head = newNode(1);
let second = newNode(2);
let third = newNode(3);
llist.head.next = second;
second.next = third;
llist.printList();
//This code is contributed by Akash Jha
Output
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>
usingnamespacestd;
intmain()
{
// Size of the Vector
intn = 5;
// Declaring the Vector with
// initial size n
vector<int> v;
// Appending new elements at
// the end of the vector
for(inti = 1; i <= n; i++)
v.push_back(i);
// Printing elements
for(autoi : v)
cout<<i<<" ";
cout<<endl;
// Remove element at index 3
v.erase(v.begin()+3);
// Displaying the vector
// after deletion
for(autoi : v)
cout<<i<<" ";
cout<<endl;
return0;
}
Java
// Java Program to Demonstrate Working
// of Vector Via Creating and using it
// Importing required classes
importjava.io.*;
importjava.util.*;
// Main class
classGFG {
// Main driver method
publicstaticvoidmain(String[] args)
{
// Size of the Vector
intn = 5;
// Declaring the Vector with
// initial size n
Vector<Integer> v = newVector<Integer>(n);
// Appending new elements at
// the end of the vector
for(inti = 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(inti = 0; i < v.size(); i++)
// Printing elements one by one
System.out.print(v.get(i) + " ");
}
}
Javascript
// Size of the Array
let n = 5;
// Declaring the Array with initial size n
let v = [];
// Appending new elements at the end of the Array
for(let i = 1; i <= n; i++)
v.push(i);
// Printing elements
console.log(v.join(" "));
// Remove element at index 3
v.splice(3, 1);
// Displaying the Array after deletion
console.log(v.join(" "));
C#
usingSystem;
usingSystem.Collections.Generic;
namespaceConsoleApp {
classProgram {
staticvoidMain(string[] args)
{
// Size of the List
intn = 5;
// Declaring the List with
// initial size n
List<int> lst = newList<int>();
// Appending new elements at
// the end of the List
for(inti = 1; i <= n; i++)
lst.Add(i);
// Printing elements
foreach(vari inlst) Console.Write(i + " ");
Console.WriteLine();
// Remove element at index 3
lst.RemoveAt(3);
// Displaying the List
// after deletion
foreach(vari inlst) Console.Write(i + " ");
Console.WriteLine();
Console.ReadKey();
}
}
}
//This code is contributed by Akash Jha
Output
[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.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!