Pros
- Super structured for matrix-like data
- Easier to group related data
- Efficient memory usage vs. Lists in some cases
When working with Salesforce Apex, arrays are one of the most straightforward ways to manage collections of data. But things get really interesting when we step into the world of multidimensional arrays. Imagine going from a flat piece of paper to a Rubik’s cube - welcome to the third dimension of thinking in Apex arrays.
Let’s break this down and talk about how multidimensional arrays (especially 3D arrays) work in Apex, how they differ from Lists, and how you can wield them like a pro.
In Apex, arrays are fixed-size containers used to store values of the same type. Think of a simple array like a row of boxes- each box has a number in it, and you can access any box using its index.
A multidimensional array, then, is an array of arrays. It’s like stacking rows on top of each other to make a grid, or going further and stacking those grids to make a cube. That’s a 3D array.
Here’s a basic example of a 2D array in Apex:
Integer[][] grid = new Integer[][] {};
Now here’s what a 3D array looks like:
Integer[][][] cube = new Integer[][][]{}; // 2 layers, 3 rows, 4 columns
Each element inside that cube is accessed with three indices: cube[layer][row][column]
.
Ever tried managing product sales data across different regions, timeframes, and categories? A single array can’t capture that complexity. With multidimensional arrays, you get the power to organize your data like a spreadsheet on steroids. You can store:
Instead of multiple arrays and spaghetti code, a single multidimensional array can bring structure and sanity.
Let’s create a 3D array to store weekly sales numbers for two regions, three product types, and four weeks.
String[][][] strings = new String[][][]{
new String[][]{
new String[]{
'a', 'b', 'c'
}
}
};
Want to initialize specific values? Sure:
System.debug(sales[0][0][0]); // 1 (Region 0, Product 0, Week 1)
System.debug(sales[0][0][1]); // 2 (Region 0, Product 0, Week 2)
System.debug(sales[0][0][2]); // 3 (Region 0, Product 0, Week 3)
Need a shortcut for initializing a smaller array with known values?
Integer[][][] matrix = new Integer[][][] {
new Integer[][] {
new Integer[] {1, 2, 3},
new Integer[] {4, 5, 6}
},
new Integer[][] {
new Integer[] {7, 8, 9},
new Integer[] {10, 11, 12}
}
};
System.debug(matrix); // (((1, 2, 3), (4, 5, 6)), ((7, 8, 9), (10, 11, 12)))
This is how you hardcode a 3D matrix - useful for test data or small datasets.
Let’s say you want to print every value in a 3D array. Time to bring out the nested loops:
Integer[][][] sales = new Integer[][][]{
new Integer[][]{
new Integer[]{
1, 2, 3
}
}
};
for (Integer i = 0; i < sales.size(); i++) {
for (Integer j = 0; j < sales[i].size(); j++) {
for (Integer k = 0; k < sales[i][j].size(); k++) {
System.debug('sales[' + i + '][' + j + '][' + k + '] = ' + sales[i][j][k]);
}
}
}
// sales[0][0][0] = 1
// sales[0][0][1] = 2
// sales[0][0][2] = 3
It’s like digging through a chest of drawers - each drawer (layer) has shelves (rows) that have boxes (columns) inside them.
Let’s simulate a scenario: You’re a Salesforce developer for a company that sells gadgets across North America and Europe. Each region tracks three products weekly over four weeks.
Here’s how you could structure that:
// Dimensions: Region x Product x SalesVolume
String[][][] sales = new String[][][]{
new String[][]{
new String[]{
'North America', 'Phones', '100'
},
new String[]{
'Europe', 'Phones', '10'
}
},
new String[][]{
new String[]{
'North America', 'Laptops', '20'
},
new String[]{
'Europe', 'Laptops', '200'
}
},
new String[][]{
new String[]{
'North America', 'Tablets', '300'
},
new String[]{
'Europe', 'Tablets', '30'
}
}
};
// Accessing the sales volume for 'North America' and 'Laptops' (Region: sales[1], Product: sales[1][0], SalesVolume: sales[1][0][2])
System.debug(sales[1][0][2]); // 20
And print a structured report:
for (Integer i = 0; i < sales.size(); i++) {
for (Integer j = 0; j < sales[i].size(); j++) {
for (Integer k = 0; k < sales[i][j].size(); k++) {
if (k == 0) {
System.debug('Region: ' + sales[i][j][k]);
} else if (k == 1) {
System.debug(' Product: ' + sales[i][j][k]);
} else {
System.debug(' Sales Volume: ' + sales[i][j][k]);
}
}
}
}
Now that’s clean, readable, and scalable.
In Apex, List<List<List<Integer>>>
is much more flexible. You can add and remove rows, columns, and layers as needed. But with arrays, you’re locked into a fixed size. That’s the trade-off for performance and structure.
If you’re sure about your data dimensions ahead of time, arrays are simpler and faster. But if things are going to change often, Lists might be the better bet.
For this article, though, we’re keeping it pure arrays.
Multidimensional arrays in Apex aren’t just a theoretical exercise - they’re a practical, powerful tool for structuring complex data. While they might not have the flexibility of Lists, their predictability and performance make them ideal when you know your data shape in advance.
So next time you’re modeling something in Apex, ask yourself: can I think in 3D?