Arrays in Apex

Arrays in Apex Salesforce

Array can hold collection of similar elements.  Each element in the array is located by index.
Index value starts with zero. So the first element occupies the index zero.

Syntax:
datatype[] arrayName;

The square brackets [] is the notation used to declare an array where the dataType can be primitive (such as Integers or Strings), sObject (Account or Contact or ay custom object etc.,) or userdefined (variable of class type) types.

Example:

Integer[]       ages;     // 30, 40, 50..
String[]         names;   // 'ranjith', 'krishnan'
Account[]      accs;
Position__c[] pos;  // position__c is a custom object for an example

The above statements will define the array with null values. The memory has to be allocated using the keyword “new” as below.

The size of array can be set using the below syntax.

Integer[] ages = new Integer[4];  // 4 denotes the size of your array
String[]  names;
names = new String[10];           // 10 elements can be stored in this array

Account[] accs = new Account[2];
account    a1 = new account();
a1.name = 'sfdcmeet';
a1.phone = '45454';

account a2 = new account();
a2.name = 'training';
a2.phone = '1234';

accs[0] = a1;
accs[1] = a2;

//Adding elements
Integer[] ages = new Integer[5];
ages[0] = 30;
ages[1] = 40;
ages[2] = 25;
ages[3] = 50;
ages[4] = 32;

Hence, in memory the elements are placed in the below format and each element can be located by index.
——————————–
0   |  1   |   2  | 3    | 4  |
——————————–
30 | 40  |  25 | 50 | 32 |
———————————
To display the entire array values in your log.
System.debug(ages);

Display each element using “for loop”:

for(Integer i = 0; i < 5; i++){
   system.debug('i = ' + ages[i]);
}

Array can be initialized while declaring itself as below

Integer[] RollNos = new Integer[]{1001, 3003, 2002, 4004};
for(Integer i = 0; i < 4; i++){
   system.debug('RollNos = ' + RollNos[i]);
}

Array of sObject

Account a1;
a1 = new Account();
a1.name = 'Test Account1';
a1.Industry = 'Education';

//The above statements can be replaced with single statement as below.
Account a1 = new Account(Name='Test Account1', Industry='Education');
Account a2 = new Account(Name='Test Account2', Industry='Apparel');
Account[] accArray = new Account[]{a1, a2 };

system.debug(accArray);

forEach loop in apex to iterate collections.
syntax:
for(dataType variable: array){

}

//Example 1
Integer[] ages = new Integer[]{30,40,20,50,32};

// syntax of for each loop -> for(dataType variable: array){}
for(Integer a: ages){    //value="{!ages}" var="a"
system.debug(a);
}
//size method to get the size of an array
for(Integer i = 0; i < ages.size(); i++){  //size() - to get the size of array
system.debug('i = ' + ages[i]);
}
//Example 2
String[] names = new String[]{'Sam','Vinesh','Ameen'};
for(String s:names){
system.debug(s);
}
//Example 3 - Array of sObject
Account a1 = new Account(Name='CTS', Phone='123');
Account a2 = new Account(Name='TCS', Phone='345');
Account[]  accs = new Account[]{a1, a2};

for(Account a: accs){
system.debug('Name : ' + a.name + '; ' + 'Phone : ' + a.phone);
}

system.debug(accs.size());

To conclude, Arrays retains the order of elements, accessed by index, accepts duplicates.

Note: 
Please be noted that Arrays of arrays are not allowed. So arrays in Salesforce can only be an one-dimensional. In Apex, the array is a notation used to declare single dimensional list. List is a type of collection type in apex.

2 thoughts on “Arrays in Apex

  1. Shubham Bangad

    I was searching for such a simple document since many days. You have really explained in simple and clear manner. Really beneficial. Although, its simple but its quite beneficial because very few people know such scratch coding. Thanks a lot!!

Leave a Reply to Shubham Bangad Cancel reply