পৃষ্ঠাসমূহ

.

Search Your Article

Total Pageviews

Wednesday, January 25, 2017

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
Syntax:
<String> [] arrayOfProducts = new List<String>();
Example:
Suppose, we would like to store name of our Products, then we could use the Array in which we could store the Product Names as shown below. You could access the particular Product by specifying the index.
//Defining array
String [] arrayOfProducts = new List<String>();
//Adding elements in Array
arrayOfProducts.add('HCL');
arrayOfProducts.add('H2SO4');
arrayOfProducts.add('NACL');
arrayOfProducts.add('H2O');
arrayOfProducts.add('N2');
arrayOfProducts.add('U296');

for (Integer i = 0; i<arrayOfProducts.size(); i++) {
    //This loop will print all the elements in array
    system.debug('Values In Array: '+arrayOfProducts[i]);   
}
Accessing array element by using index:
You could 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]);

No comments:

Post a Comment