Dynamic Lists with ArrayList
Grow a buffer backed by an allocator.
Dynamic Lists with ArrayList is a free Zig Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
A Growable Buffer
Sometimes you do not know the size up front. std.ArrayList is a dynamic array that grows as you add items, backed by an allocator. 📈
It Is Generic
ArrayList is a generic type: you name the element type once, and you get a list of exactly that type with full type safety.
const List = std.ArrayList(i32);Create It with init
You build a list with init, handing it the allocator it will use to grow. The allocator stays with the list for its whole life.
var list = std.ArrayList(i32).init(allocator);Always deinit
A list owns heap memory, so pair init with deinit to free it. A defer right after init makes that cleanup automatic.
var list = std.ArrayList(i32).init(a);
defer list.deinit();Append Items
Add one element with append. It may need to grow the buffer, so it can fail and is therefore called with try.
try list.append(7);Reach the Data
The current contents live in the .items slice. You read, index, and iterate that slice just like any other Zig slice.
for (list.items) |n| std.debug.print("{}\n", .{n});Length and Capacity
The length is list.items.len: how many values you stored. Capacity is the reserved room, which may be larger to avoid frequent regrows.
Reserve Ahead
If you know a size roughly, call ensureTotalCapacity first. Reserving once avoids repeated reallocation as the list fills up.
try list.ensureTotalCapacity(100);Remove Items
Take the last element off with pop, or remove a middle one by index. The list shrinks its length but keeps its capacity.
const last = list.pop();Hand Off Ownership
Call toOwnedSlice to take the buffer out as a plain slice. The list empties, and you become responsible for freeing it.
const slice = try list.toOwnedSlice();Built on the Basics
ArrayList is just alloc, copy, and free done for you. It proves how far explicit allocators go with a tidy, reusable type. ✅
Quick Check
Recall what you must do when an ArrayList goes out of use.
Recap
You learned that std.ArrayList grows dynamically with an allocator: init, append with try, read .items, and always deinit when done. 🎯
Frequently asked questions
Is the “Dynamic Lists with ArrayList” lesson free?
Yes — the full text of “Dynamic Lists with ArrayList” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Dynamic Lists with ArrayList”?
Grow a buffer backed by an allocator. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Dynamic Lists with ArrayList” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Zig Has No Hidden Allocations
- The Allocator Interface
- alloc, free, create, destroy
- Dynamic Lists with ArrayList