← Back to Week 3 Hub

Array vs ArrayList

Same idea — a collection of items. But one has a fixed size and the other can grow. Try adding and removing from each and watch what happens.

The core difference: an array has a fixed size set when you create it — if you need to add a 6th item to a 5-slot array, you're stuck. An ArrayList grows and shrinks as you add and remove items. Use an array when the size is known and will never change; otherwise use an ArrayList.

Array fixed size

String[] kids = new String[5]; // 5 slots, forever
kids[0] = "Natalie";
length: 5 filled: 0
String[] kidsindex 0 → 4
ArrayIndexOutOfBoundsException

ArrayList dynamic

ArrayList<String> kids = new ArrayList<>();
kids.add("Natalie"); // grows as needed
size(): 0 capacity: grows automatically
ArrayList<String> kidsno fixed size

Operation-by-operation comparison

OperationArrayArrayList
Create new String[5] new ArrayList<String>()
Size / length kids.length kids.size()
Get item kids[0] kids.get(0)
Set item kids[0] = "Ezra" kids.set(0, "Ezra")
Add item can't — size fixed kids.add("Ezra")
Remove item can't — leaves null kids.remove(0) — shifts others
Holds primitives (int, double)? yes — int[] nums no — only objects (Integer, Double)
Import needed? no java.util.ArrayList

Tip: try adding 6 names to the array. On the 6th, it throws. Try the same on the ArrayList — it just keeps going.

← DateTimeFormatter Playground ArrayList Operations →