Java LinkedList vs ArrayList: Nodes or an Array
An ArrayList is one block of cells; a LinkedList is a chain of nodes with next and prev links. Orders ORD-101 to ORD-106 show addFirst, getLast, removeFirst and remove(1), why get(2) walks two hops, and why a front insert moves three values in one and changes two links in the other.
What you will learn
- Describe how an ArrayList stores cells and a LinkedList stores linked nodes
- Use addFirst, addLast, getFirst, getLast, removeFirst and removeLast
- Explain why get(i) walks a LinkedList but jumps in an ArrayList
- Pick the right list for a job, and start with ArrayList by default
Before you start
- Java ArrayList
Both lists hold values in order and give you the same list methods. The difference is how they keep those values in memory: one block of cells, or a chain of nodes.
One block, or a chain of nodes
An ArrayList keeps one block of cells, each with an index. A LinkedList keeps separate nodes that can sit anywhere in memory. Each node holds two links: next, the address of the node after it, and prev, the one before. Think of a train where every wagon is coupled to its neighbours, so the list grows one node at a time.
Add at the end, the front, the middle
package org.studyeasy.s04l04;
import java.util.LinkedList;
public class Ex01_AddOrders {
public static void main(String[] args) {
LinkedList<String> orders = new LinkedList<>();
orders.add("ORD-102"); // at the end
orders.add("ORD-103");
orders.addFirst("ORD-101"); // new head node
orders.addLast("ORD-104"); // new tail node
System.out.println("Orders: " + orders);
orders.add(2, "RUSH-900"); // into the middle
System.out.println("Rush: " + orders);
System.out.println("Size: " + orders.size());
}
}
Orders: [ORD-101, ORD-102, ORD-103, ORD-104]
Rush: [ORD-101, ORD-102, RUSH-900, ORD-103, ORD-104]
Size: 5
addFirst makes a new head and addLast a new tail. add(2, ...) links a new node into the middle, so RUSH-900 sits at index 2.
Read and replace
LinkedList<String> orders = new LinkedList<>(
List.of("ORD-101", "ORD-102", "ORD-103"));
System.out.println("First: " + orders.getFirst());
System.out.println("Last: " + orders.getLast());
System.out.println("At 1: " + orders.get(1)); // walks there
orders.set(1, "GIFT-102"); // replace the value in place
System.out.println("Now: " + orders);
System.out.println("Has 103? " + orders.contains("ORD-103"));
First: ORD-101
Last: ORD-103
At 1: ORD-102
Now: [ORD-101, GIFT-102, ORD-103]
Has 103? true
getFirst and getLast are easy: a LinkedList always knows its two ends. get(1) is different. There's no cell to jump to, so it follows the links one node at a time. An ArrayList goes straight to cell 2 for get(2); a LinkedList starts at the head and makes two hops. (It starts from the nearer end, so near the tail it walks back from the tail.) contains also checks node after node.
Take nodes out, the chain closes
LinkedList<String> orders = new LinkedList<>(List.of(
"ORD-101", "ORD-102", "ORD-103",
"ORD-104", "ORD-105", "ORD-106"));
String shipped = orders.removeFirst(); // take the head
String cancelled = orders.removeLast(); // take the tail
System.out.println("Shipped: " + shipped);
System.out.println("Cancelled: " + cancelled);
System.out.println("Now: " + orders);
orders.remove(1); // by index: ORD-103
orders.remove("ORD-104"); // by value
System.out.println("Left: " + orders);
Shipped: ORD-101
Cancelled: ORD-106
Now: [ORD-102, ORD-103, ORD-104, ORD-105]
Left: [ORD-102, ORD-105]
When remove(1) takes out ORD-103, ORD-102 points its next link straight to ORD-104, and ORD-104 points its prev link back to ORD-102. Only two links change and nothing moves. ORD-103 is no longer reachable, so it's gone.
Same insert, different work
List<String> start = List.of("ORD-101", "ORD-102", "ORD-103");
ArrayList<String> inCells = new ArrayList<>(start);
inCells.add(0, "RUSH-900"); // cells move right
LinkedList<String> inNodes = new LinkedList<>(start);
inNodes.addFirst("RUSH-900"); // new head, two links
System.out.println("ArrayList: " + inCells);
System.out.println("LinkedList: " + inNodes);
ArrayList: [RUSH-900, ORD-101, ORD-102, ORD-103]
LinkedList: [RUSH-900, ORD-101, ORD-102, ORD-103]
Same result, different work. The ArrayList moves ORD-103, then 102, then 101 one cell right, three values moved, before RUSH-900 drops into cell 0. The LinkedList creates a new head whose next link points to ORD-101, and ORD-101's prev link points back. Two links changed.
Which one for which job
| Job | ArrayList | LinkedList | Winner |
|---|---|---|---|
| Read by position | jumps | walks node by node | ArrayList |
| Replace a value | jumps, then sets | walks, then sets | ArrayList |
| Add at the end | fine | fine | either |
| Add at the front | shifts every cell | new head node | LinkedList |
| Remove from the middle | shifts cells left | relinks two nodes, once it has walked there | LinkedList |
| Memory per item | just the value | value plus two links | ArrayList |
Most programs are fine with an ArrayList, so start there. Reach for a LinkedList when the front of the list changes a lot. An AI can suggest a data structure, but knowing how each one works lets you judge its answer.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Java equals and hashCode: Why a HashSet Needs Both
== asks if it's the very same object, equals compares values, and hashCode puts equal objects in the same HashSet bucket. Three small programs show what breaks when hashCode is missing.
From Arrays to Collections: The Java Collections Framework Map
An array of 3 students can't seat John, so Java throws Index 3 out of bounds. A List just grows. Then the map of the framework: List, Set and Queue under Collection, Map beside it, four behaviours in one program, and why the variable type should be the interface.
Java Wrapper Classes and Autoboxing
List<int> won't compile because a list holds objects only. Integer.valueOf puts 87 in a box and intValue takes it out, and add and get do it for you. Then two traps: 128 == 128 is false, and unboxing a null Integer throws a NullPointerException.
Java ArrayList: The Stretchy Array
add, add at an index, get, set, remove and contains on a list of courses and a cart, why Git at index 1 pushes SQL and Spring right, removing scores below 50 with an Iterator or removeIf, List.of that can't change, and the array inside that grows from 10 to 15.
