Industrial Training

Apex - Arrays

Arrays in Apex are basically the same as Lists in Apex. There is no logical distinction between the Arrays and Lists as their internal data structure and methods are also same but the array syntax is little traditional like Java.

Below is the representation of an Array of Products −
Index 0 − HCL
Index 1 − H2SO4
Index 2 − NACL
Index 3 − H2O
Index 4 − N2
Index 5 − U296

[] arrayOfProducts = new List();


Suppose, we have to store the name of our Products – we can use the Array where in, we will store the Product Names as shown below. You can access the particular Product by specifying the index.

//Defining array
String [] arrayOfProducts = new List();

//Adding elements in Array

for (Integer i = 0; i //This loop will print all the elements in array
system.debug('Values In Array: '+arrayOfProducts[i]);

Accessing array element by using index

You can access any element in array by using the index as shown below −

//Accessing the element in array
//We would access the element at Index 3
System.debug('Value at Index 3 is :'+arrayOfProducts[3]);

Hi I am Alfred.