Let’s clear something up real quick - Apex has both arrays and Lists, but they’re not exactly the same. Think of a classic array like an old-school toolbox with a fixed layout. It’s not as flexible as a List
, but it gets the job done, especially when you're working with a known number of items.
Here’s what a basic array looks like in Apex:
Integer[] numbers = new Integer[]{10, 20, 30, 40};
Yeah, that’s right - square brackets. When you see Type[]
, you're dealing with an actual array. Arrays in Apex are zero-indexed, just like in most programming languages. So numbers[0]
will give you 10
, numbers[1]
gives you 20
, and so on.
Finding the Length of an Array in Apex
So how do you figure out how many items are in your list? Easy: use the .size()
method. This is like asking, “Hey, how full is my backpack?”
String[] fruits = new String[]{'Apple', 'Banana', 'Cherry'};
System.debug('Total fruits: ' + fruits.size()); // Total fruits: 3
Boom. size()
gives you the current number of elements. No guesswork, no manual counting. Just clean, direct info. Now here’s where a lot of folks trip up: .size()
is not a property. You can’t write fruits.size
. You’ve got to call it with parentheses like a method: fruits.size()
.
The Different Ways to Loop Through Arrays in Apex
Alright, let’s talk looping. If .size()
tells you how many items you’ve got, a loop is how you get to each one. There are a few different ways to do this, and each one has its moment to shine.
The Classic For Loop
You’ve seen this one before- it’s like the Swiss Army knife of looping.
String[] fruits = new String[]{'Apple', 'Banana', 'Cherry'};
for (Integer i = 0; i < fruits.size(); i++) {
System.debug('Fruit at index ' + i + ': ' + fruits[i]);
}
// Output:
// Fruit at index 0: Apple
// Fruit at index 1: Banana
// Fruit at index 2: Cherry
Here’s what’s going on:
i
starts at 0 (because Apex arrays are zero-indexed).- It runs until
i
is less than the list’s size. - You use
fruits[i]
to grab each element.
The For-Each Loop (Enhanced For Loop)
Want something cleaner? This one reads like English.
String[] fruits = new String[]{'Apple', 'Banana', 'Cherry'};
for (String fruit : fruits) {
System.debug('Fruit: ' + fruit);
}
This is perfect for when you just want to go through everything, top to bottom. You don’t care about the index- you just want the goodies.
While Loop
While loops give you the flexibility to do more custom stuff, but you’ve gotta be careful or you’ll loop forever.
String[] fruits = new String[]{'Apple', 'Banana', 'Cherry'};
Integer index = 0;
while (index < fruits.size()) {
System.debug('Fruit: ' + fruits[index]);
index++;
}
Why use this? Maybe you want to exit early based on a condition, or pause between iterations.
Do-While Loop
Like a while
loop, but it runs at least once. Handy if you want to process something and then check the condition.
String[] fruits = new String[]{'Apple', 'Banana', 'Cherry'};
Integer idx = 0;
do {
System.debug('Fruit: ' + fruits[idx]);
idx++;
} while (idx < fruits.size());
Use this when you know your list has at least one element and you want to guarantee execution before checking.
Looping Through Arrays of SObjects
Most of the time in Apex, you’re looping through lists of SObjects. Like this:
List<Account> accounts = [SELECT Id, Name FROM Account LIMIT 5];
for (Account acc : accounts) {
System.debug('Account Name: ' + acc.Name);
}
This is the bread-and-butter of Salesforce development. You pull data with SOQL, store it in a list, and loop through it to process or display.
Need indexes too?
for (Integer i = 0; i < accounts.size(); i++) {
System.debug('Account at index ' + i + ': ' + accounts[i].Name);
}
Arrays in Apex are simple at the surface - but super powerful under the hood. Whether you’re counting items with .size()
, looping through records, or nesting your loops like a coding ninja, mastering these basics will take your Apex skills to the next level.