Arrays in Apex - often overshadowed by their flashier cousin List<T>
- are a powerful and often underappreciated tool in a developer's toolkit. Think of them as the old-school, no-frills data containers - straightforward, fast, and reliable. If you're coming from a JavaScript or Java background, you might already be familiar with how arrays behave, but Apex adds its own flavor.
So, let's dive right into some must-know, extremely practical array techniques for Salesforce Apex developers. We're talking raw arrays like this:
String[] fruits = new String[]{'Apple', 'Banana', 'Cherry'};
Reversing an Array
Ever had to flip an array? Maybe you're working with a sorted array and want the reverse order for display or logic. Here’s how to reverse an Apex array manually:
String[] fruits = new String[]{'Apple', 'Banana', 'Cherry'};
String[] reversed = new String[fruits.size()];
for (Integer i = 0; i < fruits.size(); i++) {
reversed[i] = fruits[fruits.size() - 1 - i];
}
System.debug(reversed); // ['Cherry', 'Banana', 'Apple']
Clean, straightforward, and zero external methods needed. Just pure loop magic.
Finding the Index of an Element (indexOf
Equivalent)
Apex doesn’t give you a built-in indexOf()
for arrays. But hey, who needs handouts when you've got loops?
String[] fruits = new String[]{'Apple', 'Banana', 'Cherry'};
String target = 'Banana';
Integer foundIndex = -1;
for (Integer i = 0; i < fruits.size(); i++) {
if (fruits[i] == target) {
foundIndex = i;
break;
}
}
System.debug('Found at index: ' + foundIndex); // 1
Why use brute force? Because it works- and in Apex, that’s sometimes your only choice.
Filtering Elements from an Array
Need to filter out specific elements based on some criteria? You can’t .filter()
like in JavaScript, but you can build your own logic easily. Let’s say you want all fruits starting with “B”:
String[] fruits = new String[]{'Apple', 'Banana', 'Cherry'};
String[] filtered = new String[0];
for (String fruit : fruits) {
if (fruit.startsWith('B')) {
filtered.add(fruit);
}
}
System.debug(filtered); // ['Banana']
Simple and effective. Remember: arrays in Apex can grow dynamically if you initialize them with new String[0]
.
Merging Two Arrays
Combining arrays isn't as easy as concat()
in some languages, but it’s totally doable:
String[] a = new String[]{'A', 'B'};
String[] b = new String[]{'C', 'D'};
String[] combined = new String[a.size() + b.size()];
for (Integer i = 0; i < a.size(); i++) {
combined[i] = a[i];
}
for (Integer j = 0; j < b.size(); j++) {
combined[a.size() + j] = b[j];
}
System.debug(combined); // ['A', 'B', 'C', 'D']
This gives you full control over the merging logic. Want to insert a separator in between? Easy- just add another line in the middle.
Creating a Subset (Slice)
Say you want a slice of an array - a subset between two indices.
String[] fruits = new String[]{'Apple', 'Banana', 'Cherry', 'Date'};
Integer startIndex = 1;
Integer endIndex = 3;
String[] subset = new String[endIndex - startIndex];
for (Integer i = 0; i < endIndex - startIndex; i++) {
subset[i] = fruits[startIndex + i];
}
System.debug(subset); // ['Banana', 'Cherry']
No built-in slice()
in Apex arrays? No problem - we build our own.
Checking if an Array Contains a Value
Want to know if a value exists in an array?
String[] fruits = new String[]{'Apple', 'Banana', 'Cherry'};
Boolean hasCherry = false;
for (String fruit : fruits) {
if (fruit == 'Cherry') {
hasCherry = true;
break;
}
}
System.debug('Contains Cherry? ' + hasCherry); // true
This is your DIY .includes()
or .contains()
- and it’s super performant for small to medium-sized arrays.
Cloning an Array
Sometimes, you need to work with a copy of the array so you don't mess up the original. Easy peasy:
String[] fruits = new String[]{'Apple', 'Banana', 'Cherry'};
String[] clone = new String[fruits.size()];
for (Integer i = 0; i < fruits.size(); i++) {
clone[i] = fruits[i];
}
System.debug(clone); // ['Apple', 'Banana', 'Cherry']
It’s a line-by-line copy, not a reference. So, if you change clone
, the original fruits
array remains untouched. That’s crucial in many scenarios.
Conclusion
Arrays in Apex might not be trendy, but they’re undeniably useful. They're like manual tools in a toolbox - sometimes, the job doesn’t need a power drill when a good old screwdriver will do just fine.
From reversing to merging, filtering to slicing, we’ve just walked through the top array techniques you should have on speed dial. These aren't just good-to-know - they’re must-know for any serious Apex developer.