How do I use a list in salesforce?

Jagdish Dhus
0

How do I use a list in Salesforce?



What is a List in Salesforce?

Ans -   a. List Class are used to store the elements inside the collection in the same order in which 
                they were inserted.

            b. List class allows us to store the duplicate elements inside it.

            c. In List Elements is stored with the help of an index.


Syntax of List Class -

            List<Datatype> objectName=new List<Datatype>();

e.g -

List<String> stateName=new List<String>();


Methods in List Class -

1. Add(element_name) - used to add a new element in the collection.

2. AddAll(collection_name) - used to add a collection of elements to the list collection at a time. 

3. Add(Integer_Index_Position, Element_Name) - used to insert the element at the specified index position.

4. size() - it returns the size of the collection.

5. isEmpty() - used to check collection is empty or not.

6. sort() - used to sort the elements in ascending order.

7. remove(index) -  remove the list element at a specified index.

8. clear() - used to remove all elements from the List.

9. Get(Index_Position) - returns the List element for a specified index.

10. Set(index_position, List_Element) - used to replace a List element at a specified index.

11. Clone - used to create a copy of the collection.

12. Contains(Element_Name) - used to Find the element in Collection.

13. IndexOf(ElementName) - used to return the index of the element.

e.g -

List<Integer> countryCodes=new List<Integer>();
countryCodes.add(91);                                 //91 for India
countryCodes.add(54);                               //54 for Argentina
countryCodes.add(61);                              //61 for Australia
countryCodes.add(975);                               //975 for Bhutan
countryCodes.add(95);                             //95 for Myanmar
countryCodes.add(92);                              //92 for Pakistan
countryCodes.add(7);                              //7 for Russia

System.debug('Country Code : ' +countryCodes);

countryCodes.add(3,65);                             //65 for Singapor
System.debug('After inserting 65 :' + countryCodes);

countryCodes.sort();
System.debug('After Sorting the country Codes :' +countryCodes);

System.debug('Total Number of elements in countryCodes is:' +countryCodes.size());


You Can Also Watch the Below Video of the List Class - 


Post a Comment

0Comments
Post a Comment (0)
To Top