[Apex] How do I create and use multi-dimensional arrays in Apex?

To create and use multi-dimensional arrays in Apex, you can follow these steps:

  1. Declare the multi-dimensional array variable with the desired data type. Example: DataType[][] multiArray;

  2. Initialize the multi-dimensional array with the desired number of rows and columns. Example: multiArray = new DataType[rows][columns];

  3. Assign values to the individual elements of the array using the index notation. Example: multiArray[rowIndex][columnIndex] = value;

  4. Access the values stored in the multi-dimensional array using the index notation. Example: DataType element = multiArray[rowIndex][columnIndex];

Here's an example of creating and using a multi-dimensional array in Apex:

 1// Declare the multi-dimensional array
 2Integer[][] multiArray;
 3
 4// Initialize the multi-dimensional array
 5multiArray = new Integer[3][2];
 6
 7// Assign values to the elements of the array
 8multiArray[0][0] = 1;
 9multiArray[0][1] = 2;
10multiArray[1][0] = 3;
11multiArray[1][1] = 4;
12multiArray[2][0] = 5;
13multiArray[2][1] = 6;
14
15// Access the values stored in the multi-dimensional array
16Integer element = multiArray[1][0]; // element = 3

In the above example, we create a 3x2 multi-dimensional array and assign values to its elements. We then access the value stored at the index (1, 0) which is 3.

Note: The data type in the example is Integer, but you can replace it with any other valid data type based on your requirements.