integer to string in apex program
You can easily convert an Integer to a String in Apex using the String.valueOf() method. Here's a simple Apex program that demonstrates this conversion:
Integer myInteger = 42; // Replace 42 with your integer value
String myString = String.valueOf(myInteger);
System.debug('Converted String: ' + myString); // Output the converted string to the debug log
Or
Integer myInteger = 221;
String sInteger = String.valueOf(myInteger);
System.assertEquals('221', sInteger);
List<AccountHistory> ahlist = [SELECT Id,Name FROM Accounts];
for(AccountHistory ah : ahlist)
{
System.debug('Field: ' + ah.Field);
if (ah.field == 'NumberOfEmployees')
{
Integer oldValue =
Integer.valueOf(ah.OldValue);
Integer newValue =
Integer.valueOf(ah.NewValue);
}
1. For Loop to Iterate Over a List:
List<String> fruits = new List<String>{'Apple', 'Banana', 'Cherry', 'Date'};
for (String fruit : fruits) {
System.debug(fruit);
}
2. For Loop to Iterate Over a Set:
Set<Integer> numbers = new Set<Integer>{1, 2, 3, 4, 5};
for (Integer num : numbers) {
System.debug(num);
}
3. For Loop to Iterate Over a Map:
Map<String, Integer> studentScores = new Map<String, Integer>{
'Alice' => 95,
'Bob' => 85,
'Charlie' => 78
};
for (String name : studentScores.keySet()) {
Integer score = studentScores.get(name);
System.debug(name + ': ' + score);
}
4. For Loop for a Set Number of Iterations:
for (Integer i = 0; i < 5; i++) {
System.debug('Iteration ' + i);
}
1. Add an Element to a List:
You can add an element to the end of a list using the add() method.
List<String> colors = new List<String>{'Red', 'Blue', 'Green'};
colors.add('Yellow');
System.debug(colors); // Outputs: (Red, Blue, Green, Yellow)
2. Remove an Element from a List:
You can remove an element from a list using the remove() method.
List<String> fruits = new List<String>{'Apple', 'Banana', 'Cherry'};
fruits.remove(1); // Removes the element at index 1 (Banana)
System.debug(fruits); // Outputs: (Apple, Cherry)
3. Get an Element by Index:
You can access an element at a specific index using square brackets.
List<Integer> numbers = new List<Integer>{1, 2, 3, 4, 5};
Integer thirdNumber = numbers[2]; // Gets the element at index 2 (3)
System.debug(thirdNumber); // Outputs: 3
4. Check If a List Contains an Element:
You can check if a list contains a specific element using the contains() method.
List<String> names = new List<String>{'Alice', 'Bob', 'Charlie'};
Boolean containsBob = names.contains('Bob');
System.debug(containsBob); // Outputs: true
5. Sort a List:
You can sort a list using the sort() method.
List<Integer> unsortedNumbers = new List<Integer>{5, 1, 4, 3, 2};
unsortedNumbers.sort();
System.debug(unsortedNumbers); // Outputs: (1, 2, 3, 4, 5)
The get() method in Apex is used to access elements in a list or array-like data structure by their index position. The index is a zero-based integer that represents the position of the element within the list. Here's an example of how to use the get() method:
List<String> fruits = new List<String>{'Apple', 'Banana', 'Cherry', 'Date'};
// Access the element at index 1 (second element)
String secondFruit = fruits.get(1);
System.debug(secondFruit); // Outputs: Banana
In Apex, the set() method is used to set the value of a specific element in a list or array-like data structure at a given index position. This allows you to update or modify the content of the list at the specified index. Here's an example of how to use the set() method:
List<String> colors = new List<String>{'Red', 'Blue', 'Green'};
// Set the value at index 1 to 'Yellow'
colors.set(1, 'Yellow');
System.debug(colors); // Outputs: (Red, Yellow, Green)
In Apex, the clear() method is used to remove all elements from a list or collection, effectively making the list empty. This method is particularly useful when you want to clear the contents of a list, making it ready for reuse or for freeing up memory. Here's an example of how to use the clear() method:
List<String> names = new List<String>{'Alice', 'Bob', 'Charlie', 'David'};
System.debug('Original List: ' + names); // Outputs: (Alice, Bob, Charlie, David)
// Clear the list
names.clear();
System.debug('Cleared List: ' + names); // Outputs: ()
Apex variables are Case-Insensitive
Integer m = 100;
for (Integer i = 0; i<10; i++) {
integer m = 1; //This statement will throw an error as m is being declared
again
System.debug('This code will throw error');
}