Beginner 20 min read

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';
  1. 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');
  1. List<String> customerNames = new List<String>(); — List<String> means "a List that holds String values specifically" (the type inside <...> says what the List is allowed to hold). new List<String>() creates a brand-new, empty List — the same new keyword you've already used for other types.
  2. customerNames.add('Acme Corp'); — .add(...) appends one item to the end of the List.
  3. customerNames.add('Globex'); — appends a second item.
  4. 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'};
  1. 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
  1. System.debug(customerNames[0]); — [0] reads the FIRST item — indexes start counting at 0, not 1, which trips up almost everyone at first.
  2. System.debug(customerNames[1]); — [1] is the SECOND item.
  3. 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.

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

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.

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Lists: A Quick Foundation Before Looping Quiz

1. What does List<String> mean?

2. In a List with 3 items, what index is the FIRST item at?

3. What does .size() return?

4. What does .add() do?

Log in to submit the quiz and save your score.

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.