Apex Code
List<string> Fruits = new List<string>
{'Banana', 'Apple', 'Cherry'};
System.debug('Fruits: '+Fruits);
Execution Log
Apex Code
List<string> Fruits = new List<string>
{'Banana', 'Apple', 'Cherry'};
System.debug('The size of the list is: '+Fruits.size());
Execution Log
Apex Code
List<string> Fruits = new List<string>
{'Banana', 'Apple', 'Cherry'};
Fruits.add('Mango');
System.debug('The size of the list is: '+Fruits.size());
Execution Log
Output: The size of the list is 4.
Apex Code
List<string> Fruits = new List<string>
{'Banana', 'Apple', 'Cherry'};
Fruits.add('Mango');
System.debug('The fouth element of the list is: '+Fruits.get(3));
or
List<string> Fruits = new List<string>
{'Banana', 'Apple', 'Cherry'};
Fruits.add('Mango');
System.debug('The fouth element of the list is: '+Fruits[3]);
Execution Log
Output: The fourth element of the list is Mango.
Apex Code
List<string> Fruits = new List<string>
{'Banana', 'Apple', 'Cherry'};
System.debug('The index of the Apple is : '+Fruits.indexOf('Apple'));
Execution Log
Apex Code
List<string> Fruits = new List<string>
{'Banana', 'Apple', 'Cherry'};
Fruits.clear();
System.debug('The number of elements of the list is: '+Fruits.size());
Execution Log
Apex Code
List<string> Fruits = new List<string>
{'Banana', 'Apple', 'Cherry'};
Fruits.set(2, 'Orange');
System.debug('The second element of the list is: '+Fruits.get(2));
Execution Log
Apex Code
List<string> Fruits = new List<string>
{'Banana', 'Apple', 'Cherry', 'Orange'};
Fruits.remove(1);
System.debug('Fruits: '+Fruits);
Execution Log
Apex Code
List<String> Colors = new List<String>
{'Red', 'Blue', 'Orange'};
System.debug(Fruits.contains('Orange'));
Execution Log
Apex Code
List<string> Fruits = new List<string>
{'Banana', 'Apple', 'Cherry'};
for (String i : Fruits) {
System.debug(i);
}
Execution Log
Apex Code
1. Way
List<string> Fruits = new List<string>
{'Banana', 'Apple', 'Cherry'};
List<string> Fruits2 = new List<String>();
for (String i : Fruits) {
Fruits2.add(i.toUpperCase());
}
System.debug(Fruits2);
2. Way
List<string> Fruits = new List<string>
{'Banana', 'Apple', 'Cherry'};
for (String i : Fruits) {
Fruits.set(Fruits.indexof(i), i.toUpperCase());
}
System.debug(Fruits);
Execution Log