What Is a List In Apex?

Apex Collections:          
          The Apex language provides developers with three classes (Set, List and Map) that make it easier to handle collections of objects. In a sense these collections work somewhat like arrays, except their size can change dynamically, and they have more advanced behaviors and easier access methods than arrays.  
Salesforce.com recently made an important change to collections. There is no longer a limit on the number of items a collection can hold.
LIST:
    A list is an Ordered collection of typed primitives, sObjects, user-defined objects, Apex objects or collections that are distinguished by their indices. 
For Example: the following table is a visual representation of a list of Strings:
 The index position of the first element in a list is always Starts with 0 (Zero).
Because lists can contain any collection, they can be nested within one another and become multidimensional. 
Note: A list can only contain up to five levels of nested collections inside it.

List Syntax:
To declare a list, use the List keyword followed by the primitive data, sObject, nested list, map, or set type within < > characters.
List<datatype> listname;
Creating an empty list of String:
// Create an empty list of String 
    List<String> my_list = new List<String>();
// Create a nested list 
    List<List<Set<Integer>>> my_list_2 = new List<List<Set<Integer>>>();
// Create a list of account records from a SOQL query 
    List<Account> accs = [SELECT Id, Name FROM Account LIMIT 1000];
List Methods:
           The list methods are all instance methods, that is, they operate on a particular instance of a list. Now we will see the all the list methods.

ADD Method:  Adds elements to a list.
// Add single integer element to list
List<Integer> numlist = new List<Integer>();
numlist.add(23);

// Add multipule elements to list
List<Integer> numlist = new List<Integer>(30,20,1);

// Add Strings to list
List<String> namelist = new List<String>();
namelist.add('Nagarjun');
namelist.add('Malli');
namelist.add('Sreenu');

// Add s-Object to list
List<Account> acclist = new List<Account>();
Account acc = New Account();
Acc.name = 'Nagarjun';
acclist.add(acc);

You Might Also Like: What Is a SET In Apex?
SET Method: Adds elements to a list.
// Set the inter to list
List<Integer> numlist = new List<Integer>();
numlist.set(0,10); // Add's 10 at the first position. 
numlist.set(1,20); // Add's 20 at the second position.

//This can also be achieved with the following:-
Numlist[0] = 10;
Numlist[1] = 20;

// Set the Strings to list
List<String> namelist = new List<String>();
namelist.set(0,'Nagarjun');
namelist.set(1,'Hari');
namelist.set(2,'Krishna');

GET Method: Retrieves element from a list.
// Get the Elements from List
List<Integer> numlist = new List<Integer>();
numlist.add(23);
Integer Mynum = numlist.get(0) //Retrieves first occurrence from the numlist. 

// Get the Strings from List
List<String> namelist = new List<String>();
namelist.add('Nagarjun');  //Added Mark at first occurrence.
namelist.add('Hari'); //Added Frank at Second occurrence.
namelist.add('Krish');
String Myname = namelist.get(1) // Retrieves second occurrence i.e 'Hari' from the namelist.

Remove Method: Remove element from a list.
// Remove Integer from list
List<Integer> numlist = new List<Integer>(20,10,30);
numlist.remove(0);  //Removes first occurrence i.e 20 from the numlist. 
// Remove Strings from List
List<String> namelist = new List<String>('Nagarjun','Malli','Hari');
Namelist.remove(2) //Removes third occurrence i.e 'Hari' from the namelist.

Clear Method: Removes all the elements from the list and sets the size of the list to Zero.
// Remove all Integer elements from list
List<Integer> numlist = new List<Integer>(20,10,30);
numlist.clear(); // Removes all elements from the numlist. 

//Removes all Strings from the list.
List<String> namelist = new List<String>('Malli','Hari','Krish');
Namelist.clear() //Removes all names from the namelist.

Clone Method: Creates a new list and adds all the elements of an existing list into that new list.
// Clone the integer Elements
List<Integer> numlist = new List<Integer>(20,10,30);
List<Integer> newnumlist = numlist.clone(); //Creates new list called newnumlist and adds all the elements into it from numlist. 

// Clone the String Elements
List<String> namelist = new List<String>('Nagarjun','Hari','malli');
List<Integer> newnamelist = namelist.clone(); //Creates new list called newnumlist and adds all the elements into it from namelist.

Note: If this method is used on List of Sobject records it will do only perform a shallow clone.
List<Account> acclist = new List<Account>(a1,a2,a3);
List<Account> newacclist = acclist.clone() //Creates a new list newacclist and virtually creates the new elements a1,a2,a3 into it.

Deepclone Method: Creates a new list and adds all the elements of an existing list into that new list.  This method can be used only for Sobject lists and not for primitive lists.
// Deeclone the s-objects
List<Account> acclist = new List<Account>{a1,a2,a3};
List<Account> newacclist = acclist.deepclone()  //Creates a new list newacclist and actually adds the new elements a1,a2,a3 into it.

IsEmpty Method: Boolean method which returns true if the list has no elements.
// Clear the List
List<String> namelist = new List<String>{'Hari','Rakesh','Suneel'}; namelist.clear();  //As all the elements are now removed from the namelist using the clear method.

Size Method: Return the number of the elements stored in the list.
// Size of the List
List<String> namelist = new List<String>{'Hari','Rakesh','Suneel'}; 
Integer I = namelist.size(); // integer I have size of the list, Number of elements in namelist are 3

Sort Method: Arranges the elements stored in the list.
// Sort the element in the list
List<String> namelist = new List<String>{'Nagarjun','Malli','kishor'};
namelist.sort();

COMMENTS

BLOGGER
Name

Apex Apex Default Methods Apex Methods Apex_Collections Apps Batch Apex Books CRM eBooks Data Import Wizard Data Management DataLoader Dataloader.io Difference Between Error in Salesforce FAQ's FAQ's-Apex FAQ's-Apps FAQ's-CRM FAQ's-Data loader FAQ's-SOQL & SOSL FAQ'S-Triggers FAQ's-User Profile & Security FAQ's-VF Force.com Explorer Force.com Ide Formulas & Functions Integration Interview Questions Only Latest_Updates Limits&Best Practices Online-Training Reports and Dashboards Salesforce Deployment Salesforce Realtime Examples On Development Salesforce Realtime Task On Admin Salesforce Realtime Tasks Salesforce Realtime Tasks- Apex Salesforce Training Salesforce1 Mobile Sites SOQL Spring'14 Release Triggers User Profile & Security VF Tags VisualForce Winter'14 Release Wizard for Accounts/Contacts Workbench Workflows and Approvals
false
ltr
item
Sfdc Gurukul- All in one place for salesforce and force.com step by step tutorial for beginners: What Is a List In Apex?
What Is a List In Apex?
What Is a List In Apex?, List in apex programming language,how to use list in apex programming
http://2.bp.blogspot.com/-fQ4q-x-OTP8/UzfsW0d-6gI/AAAAAAAABog/VP5chstXYBo/s1600/Apex_List.jpg
http://2.bp.blogspot.com/-fQ4q-x-OTP8/UzfsW0d-6gI/AAAAAAAABog/VP5chstXYBo/s72-c/Apex_List.jpg
Sfdc Gurukul- All in one place for salesforce and force.com step by step tutorial for beginners
http://sfdcgurukul.blogspot.com/2013/07/what-is-list-in-apex.html
http://sfdcgurukul.blogspot.com/
http://sfdcgurukul.blogspot.com/
http://sfdcgurukul.blogspot.com/2013/07/what-is-list-in-apex.html
true
4199533888133360731
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy