List Examples Part1

List Initialization Examples Part1:

1. Create List of Account.

List<Account> accs = new List<Account>();

2. Create a list to store salaries.

List<Decimal> salaries = new List<Decimal>();

3. Create a list to store city names

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

4. Create a list to store contacts

List<Contact> contacts  = new List<Contact>();

5. Create a list to store StudentWrapper objects (Where StudentWrapper is a wrapper class) and this is an example for list of user defined data type.

List<StudentWrapper> students = new List<StudentWrapper>();

Static Allocation

If we assign the values at the time of declaring a variable, it is called as list in static allocation

List<String> names = new List<String>{'Sam','Ram','Ravi'};

0               1           2
——————————-
| Sam | Ram | Ravi |
——————————-

List<Integer> ages = new List<Integer>{10,20,30};

0          1       2
———————-
| 10 | 20 | 30 |
———————-

Examples :
1. Create a list to store city names and assign the values

List<String> cities = new List<String>{'Hyd','Ban','Che'};

0             1           2
——————————-
| Hyd | Ban | Che |
——————————-

Execute the below in “Anonymous Window” in Developer Console

for(String s:cities){
    System.debug(s);
}

2. Create a list to store student name and assign the values and print them

List<String> students = new List<String>{'Ravi','Kumar','Hari'};

0               1               2
———————————-
| Ravi | Kumar | Hari |
———————————-

Execute the below in “Anonymous Window” in Developer Console

for(String s: Students)}{
System.debug(‘Student Names:’+s);
}

List Methods :

  1. add(element) : This method will add new element at the end of the list

Example:

List<Integer> ages = new List<Integer>();
ages.add(10);

Representation of list values in memory (Each List element is located by index)
0
———
| 10 |
———-

ages.add(20);

0        1
—————–
| 10 | 20 |
——————-

ages.add(30);

0         1       2
——————-
| 10 | 20 | 30 |
——————–

Execute the below in “Anonymous Window” in Developer Console

for(Integer a:ages) {
   System.debug('Ages :'+a);
}

Examples :
1. Create a list to store the product names and display the product names

List<String> prodNames=new List<String>();
prodNames.add('IPhone');
prodNames.add('IPad');
prodNames.add('MacBook');

for(String s:prodNames){
    System.debug('ProdNames:'+s);
}

2. Create a list Account where every account should have name and phone

List<Account> accs=new List<Account>();

Account a1=new Account();
a1.Name='Capital';
a1.phone='123';

Account a2=new Account(Name='Wipro',Phone='234');

accs.add(a1);
accs.add(a2);
accs.add(a1);

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

3. Create a list of contacts and add three contacts and display all the elements

List<Contact> contacts = new List<Contact>();
Contact c1 = new Contact(LastName='Krishnan', FirstName='Ranjith');
Contact c2 = new Contact(LastName='Reddy',FirstName='Siddardh');
contacts.add(c1);
contacts.add(c2);

4. Create a list of opportunities and add three opportunities

List<Opportunity> optyList = new List<Opportunity>();
Opportunity op1 = new Opportunity( Name='Salesforce', Amount=9000);
optyList.add(op1);

for(Opportuntiy op:optyList){
   System.debug('Opportunity Name :' + op.name);
   System.debug('Opportunity Amount:' + op.amount);
}

2. add(index,element):
This method will add the new element at the given index

List<Integer> ages=new List<Integer>();
ages.add(10); // 10
ages.add(10); // 10,10
ages.add(30); // 10,10,30
ages.add(90); // 10,10,30,90

Representation of list values in memory (Each List element is located by index)

0         1      2      3
————————
| 10 | 10 | 30 | 90 |
————————

ages.add(1,80); // 10,80,10,30,90

Representation of list values in memory (Each List element is located by index)

0         1        2      3    4
—————————-
| 10 | 80 | 10 | 30 | 90 |
—————————-

ages.add(2,40); // 10, 80,40,10,30 ,90

Representation of list values in memory (Each List element is located by index)

0      1       2    3      4
——————————–
| 10 | 80 | 40 |10 | 30 | 90 |
———————————

3. addAll(list|set) :

This method will add list or set of values to a list
Example 1:

List<Integer> marks=new List<Integer>{10,20,30,40};
List<Integer> internal=new List<Integer>();
internal.add(10); // 10
internal.add(20); // 10,20
internal.addAll(marks); // 10,20,10,20,30,40
// all the elements in the marks are added to the internal

Example 2:

List<String> Cities = new List<String>{'Hyd','Bang','Chn'};
List<String> places = new List<String>();
places.add('Pune');
places.addAll(cities); // Pune, Hyd, Bang, Chn

4.get(index) :
This method will return the element which is available at the given index.

Example 1:

List<Integer> ages = new List<Integer>{10,20,30,40};

Representation of list values in memory (Each List element is located by index)

0         1      2      3
—————————-
| 10 | 20 | 30 | 40 |
—————————

Integer a=ages.get(1);
System.debug(a); // 20 
Integer b=ages.get(3); 
System.debug(b); // 40

Example 2:

List<String> names = new List<String>();
names.add('Ravi');
names.add('kiran');
names.add('hari');

String s1 = names.get(1); // Kiran
System.debug(s1);

String s2 = names.get(2);
System.debug(s2);

Example 3:

List<Account> accsList = new List<Account>();
Account a1 = new Account(Name='Test1',Phone='123');
Account a2 = new Account(Name='Test2',Phone='345');
Account a3 = new Account(Name='Test3',Phone='567');
accList.add(a1);
accList.add(a2);
accList.add(a3);

Account acc = accList.get(1);
System.debug('Name :'  + acc.Name); // Test2
System.debug('Phone :' + acc.Phone); // 345

Account a=accList.get(2);
System.debug('Name :'+a.Name);   // Test3
System.debug('Phone :'+a.phone); // 567