ARRAYS IN SALESFORCE – APEX TUTORIAL (EXAMPLES)

2
413
array in salesforce
array in salesforce examples

ARRAYS IN SALESFORCE – APEX TUTORIAL (EXAMPLES)

Hello, Friends, I am very happy to tell you all with this useful information. This post is only for Beginners. And for the experienced persons, this post helps them to refresh the things which they already know.

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 the same but the array syntax is little traditional like Java.

Below is the representation of an Array of Products −

Index 0 − ABC

Index 1 − ABCDE

Index 2 − EFG

Index 3 − HIJ

Index 4 − KL

Index 5 − MNOP

Syntax

<String> [] arrayOfProducts = new List<String>();

Example

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

//Defining array
String [] myArray = new List<String>();
//Adding elements in Array
myArray.add(‘ABC’);
myArray.add(‘ABCDE’);
myArray.add(‘EFG’);
myArray.add(‘HIJ’);
myArray.add(‘KL’);
myArray.add(‘MNOP’);
for (Integer i = 0; i<myArray.size(); i++) {
   //This loop will print all the elements in an array
   system.debug(‘Values In Array: ‘+myArray[i]);

Must Read: TwoFactor Authentication In Salesforce / Best Salesforce Security Guide.

Accessing array element by using index

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

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

Arrays and Lists:

Apex has a list collection type that holds an ordered collection of objects. List elements can be accessed with an index or can be sorted if they’re primitive types, such as Integers or Strings.
You’ll typically use a list whenever you want to store a set of values that can be accessed with an index. As you’ll see in later tutorials, lists are also used to hold the results of queries.
You can access an element of a list using a zero-based index, or by iterating over the list.  Here’s how to create a new list, and display its size:
List<Integer> intList = new List<Integer>();
System.debug(intList.size());

Arrays in Apex are synonymous with lists—Apex provides an array-like syntax for accessing lists. Here is an alternative way to create exactly the same list:

Best Explained: Multiple Currencies In Salesforce

Integer[] intList = new List<Integer>();

You can also define a list variable and initialize it at the same time as shown in the following example, which displays the string ‘two’:

List<String> myStrings = new List<String> { ‘one’, ‘two’ };

To add a new element to a list, use the add method.

intList.add(101);

You can use the array notation to get or modify an existing value.

// Get the first element
Integer i = intList[0];
// Modify the value of the first element
intList[0] = 100;

 

SALESFORCE – DATA TYPES APEX DEVELOPER GUIDE
SALESFORCE – VARIABLES APEX DEVELOPER GUIDE
APEX STRING CLASS DEVELOPER GUIDE FOR BEGINNERS

Stay tuned to salesforceupdates.com for more updates.

Thank you so much for reading my post. Feel free to ask doubts/Queries in the comment section below, and I’ll come again with an interesting topic about Salesforce So Please Share, Subscribe to our blog for the latest update.

LEAVE A REPLY

Please enter your comment!
Please enter your name here