Lists: A Quick Foundation Before Looping
By the end of this lesson, you'll be able to:
- Explain what a List is and why you need one before you can loop over anything
- Declare an empty List and add items to it
- Read an item out of a List by its index
- Use .size() to find out how many items a List holds
Prerequisites: Module 6: "Making Decisions"
The problem: one variable only holds one value
Every variable you've written so far holds exactly one value:
String customerName = 'Acme Corp';
String customerName = 'Acme Corp';— one variable, one value.
But real Salesforce work is rarely about one record — it's dozens, hundreds, or thousands of Accounts, Cases, or Contacts at once. You need a single variable that can hold many values together. That's what a List is for.
Declaring and filling a List
List<String> customerNames = new List<String>();
customerNames.add('Acme Corp');
customerNames.add('Globex');
customerNames.add('Initech');
List<String> customerNames = new List<String>();—List<String>means "a List that holdsStringvalues specifically" (the type inside<...>says what the List is allowed to hold).new List<String>()creates a brand-new, empty List — the samenewkeyword you've already used for other types.customerNames.add('Acme Corp');—.add(...)appends one item to the end of the List.customerNames.add('Globex');— appends a second item.customerNames.add('Initech');— appends a third.
A List can hold any type — List<Integer>, List<Boolean>, even List<Account> once you've learned about Salesforce records in Apex — but the idea is identical every time: one variable, many values of the same type.
A shortcut: filling a List in one line
List<String> customerNames = new List<String>{'Acme Corp', 'Globex', 'Initech'};
List<String> customerNames = new List<String>{'Acme Corp', 'Globex', 'Initech'};— produces the exact same List as three separate.add()calls — just written in one line, with the starting values listed inside{ }.
You'll see this shorthand constantly from here on, since it's more compact once you already know what values a List should start with.
Reading items back out: index and size
System.debug(customerNames[0]); // 'Acme Corp' — the FIRST item
System.debug(customerNames[1]); // 'Globex' — the SECOND item
System.debug(customerNames.size()); // 3 — how many items are in the List
System.debug(customerNames[0]);—[0]reads the FIRST item — indexes start counting at0, not1, which trips up almost everyone at first.System.debug(customerNames[1]);—[1]is the SECOND item.System.debug(customerNames.size());—.size()returns how many items are in the List right now —3— which is exactly what the rest of this module's loops need in order to know when to stop.
Why this comes before loops
Every lesson from here on in this module shows you how to run the same block of code once for every item in a List, automatically, instead of writing customerNames[0], customerNames[1], customerNames[2]... by hand. That only makes sense once you can already read and write a List yourself — which is exactly what this lesson just taught. The full "Collections: Lists" module, later in the course, goes much deeper (sorting, more iteration patterns, and List's siblings Set and Map) — this lesson is only the minimum foundation this module needs right now.
Exercise
Declare a List<String> called colors, add three color names to it one at a time using .add(), then debug the second color using its index.
Show hint
Remember: the second item has index 1, not 2, because indexes start at 0.
Exercise
Given List<Integer> scores = new List<Integer>{85, 92, 78};, debug the List's size and debug the LAST score using its index.
Show hint
With 3 items, the last item's index is 2, not 3 — one less than the size.
Lists: A Quick Foundation Before Looping Quiz
Leaderboard
- 1. Monde Masiza 4/4
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Every loop in this module runs "once per item in a List" — so before learning to loop, you need a List to loop over. This lesson teaches just enough to read and write one.