ArrayList: The Dynamic Array
An ArrayList is like a dynamic array.
It stores elements by their index, so you can quickly jump to any element you want. It can hold all sorts of elements, even null, and it’s totally fine with duplicates.
Some operations in an ArrayList are super fast and take the same amount of time no matter how many elements you've got. These include set, get, iterator, ListIterator, isEmpty, and size.
Think of an ArrayList like a resizable array.
It stores elements in a contiguous block of memory (like seats in a theater). If you know the position of an element, you can instantly access it — just like finding your seat by number.
when:
- You need fast random access
- You rarely insert or remove elements in the middle
- You deal with fixed or moderately changing lists
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> shoppingList = new ArrayList<>();
// Adding items to the shopping list
shoppingList.add("Balloons");
shoppingList.add("Streamers");
shoppingList.add("Cake");
shoppingList.add("Ice Cream");
shoppingList.add("Soda");
System.out.println("Shopping list for the party:");
System.out.println(shoppingList);
// Accessing an item by index
String cake = shoppingList.get(2);
System.out.println("Gotta make sure we got the cake: " + cake);
// Checking if the list is empty
boolean isEmpty = shoppingList.isEmpty();
System.out.println("Is the shopping list empty? " + isEmpty);
// Checking the size of the list
int size = shoppingList.size();
System.out.println("Number of items on the list: " + size);
// Finding the index of "Ice Cream"
int iceCreamIndex = shoppingList.indexOf("Ice Cream");
System.out.println("Ice Cream is at index: " + iceCreamIndex);
// Removing an item (uh oh, someone decided no soda!)
shoppingList.remove("Soda");
System.out.println("Shopping list after removing Soda:");
System.out.println(shoppingList);
// Iterating through the list with a for-each loop
System.out.println("Checking off the items:");
for (String item : shoppingList) {
System.out.println("Got " + item);
}
// Clearing the list after the party
shoppingList.clear();
System.out.println("Is the shopping list empty after the party? " + shoppingList.isEmpty());
// Adding a new item post-party (we forgot to clean up!)
shoppingList.add("Cleaning Supplies");
System.out.println("Post-party shopping list:");
System.out.println(shoppingList);
}
}
LinkedList: The Chain Connection
Data structure used: Doubly linked list
The LinkedList is a dynamic data structure and part of the Java Collections Framework. It implements a doubly linked list, meaning each element (node) stores the actual data and references to both the previous and next nodes in the sequence.
A LinkedList, on the other hand, is like a chain of train cars — each car (called a node) holds data and a link to the next and previous cars.
when:
- You frequently add or remove elements
- You don’t need fast random access
- You’re implementing queues, stacks, or iterative traversals
import java.util.LinkedList;
public class LinkedListt {
public static void main(String[] args) {
LinkedList<String> queue = new LinkedList<>();
// People join the line
queue.add("Alice");
queue.add("Bob");
queue.add("Charlie");
queue.add("Diana");
queue.add("Eve");
System.out.println("The line for ice cream:");
System.out.println(queue);
// Peek at the first person in line without removing them
String firstPerson = queue.peek();
System.out.println("First person in line (peek): " + firstPerson);
// Bob gets impatient and leaves
queue.remove("Bob");
System.out.println("Line after Bob leaves:");
System.out.println(queue);
// Serve the first person in line
String servedPerson = queue.removeFirst();
System.out.println(servedPerson + " got served ice cream!");
// Peek again to see who's next
String nextPerson = queue.peek();
System.out.println("Next person in line (peek): " + nextPerson);
// Serve the next person
servedPerson = queue.removeFirst();
System.out.println(servedPerson + " got served ice cream!");
// Check the size of the line
int size = queue.size();
System.out.println("Number of people left in line: " + size);
// Clear the line because the ice cream shop ran out of ice cream
queue.clear();
System.out.println("Is the line empty now? " + queue.isEmpty());
// Adding a new line just for fun
queue.add("Frank");
System.out.println("Frank is now first in line:");
System.out.println(queue);
// Remove first and last (only Frank in this case)
queue.removeFirst();
System.out.println("Is the line empty after Frank is served? " + queue.isEmpty());
}
}
Top comments (0)