目次
Write the code according to the following conditions.
- Declare a variable
numbers oftypeListand store the six values5,12,18,25,32,40.- Create a new list
filteredNumbersthat contains only values where each element in the list is greater than or equal to20.- Calculate the sum of the
filteredNumbersand output the value to the console usingSystem.debug().
Solution Example
public class ListFilterExample {
public static void main() {
// Declare a variable numbers of type List and store 6 values
List<Integer> numbers = new List<Integer>{5, 12, 18, 25, 32, 40};
// create a new list filteredNumbers and filter out values greater than 20
List<Integer> filteredNumbers = new List<Integer>();
for (Integer num : numbers) {
if (num >= 20) {
filteredNumbers.add(num);
}
}
// calculate the sum of filteredNumbers
Integer sum = 0; }
for (Integer num : filteredNumbers) {
sum += num;
}
// output the total to the console
System.debug('Sum of numbers >= 20: ' + sum);
}
} Explanation
The program extracts values greater than or equal to 20 from a list, calculates the sum of the values, and displays the result on the console.
1. Declaration and Initialization of a List
First, declare the list and store the values.
List<Integer> numbers = new List<Integer>{5, 12, 18, 25, 32, 40};Listrepresents a list of integers, whereInteger is adata type that handles integers andList isa data type that can handle multiple values combined into one.numbersis the name of the list, which contains six integer values (5, 12, 18, 25, 32, 40).
2. Creating a new list filteredNumbers
Next, we create a new list filteredNumbers that contains only numbers “greater than or equal to 20” from the list.
List<Integer> filteredNumbers = new List<Integer>();- Create an empty list
filteredNumbers. This is a list that will later be used to include only numbers that match the criteria.
3. Filtering values using a for loop
Next, the contents of the list numbers are checked in order, and only numbers greater than 20 are added to the new list filteredNumbers.
for (Integer num : numbers) {
if (num >= 20) {
filteredNumbers.add(num);
}
}- For loop: The contents of
numbersare taken out one by one and put into a variable callednum, which is then processed. This is done for all values in the list (5, 12, 18, 25, 32, 40).- In the first loop,
numis set to 5. - In the second loop,
numbecomes 12. - In the third loop,
numbecomes 18. - …and so on.
- In the first loop,
- if statement: The conditional statement
if (num >= 20)checks ifnumis greater than or equal to 20.- For example, in the first loop,
numis 5, so this condition is not satisfied. Therefore, nothing is done. - In the fourth loop,
numis 25, so this condition is satisfied. 25 is added tofilteredNumbers.
- For example, in the first loop,
filteredNumbers.add(num)adds the number that satisfies the condition (20 or more) tofilteredNumbers.
As a result, filteredNumbers becomes a list with three numbers [25, 32, 40].
4. Calculating totals
Next, we calculate the sum of the numbers in filteredNumbers.
Integer sum = 0;
for (Integer num : filteredNumbers) {
sum += num;
}- sum: Create a variable
sum, initially set to 0. We add the numbers in the list to this variable. - for loop: Now we take the contents of
filteredNumbers(25, 32, 40) in order and add the numbers tosum.- In the first loop,
numis 25, so we add 25 tosum.sumbecomes 0 25 = 25. - In the second loop,
numis 32, sosumis 32.sumis 25 32 = 57. - In the third loop,
numis 40, sosumis 40.sumis 57 40 = 97.
- In the first loop,
Finally, sum is 97. This is the total value.
5. Output the result to the console
Finally, the sum is displayed on the console with System.debug.
System.debug('Sum of numbers >= 20: ' + sum);System.debug()is an instruction to print a message to the console.sumappends the sum (97) to the string and displays it together.
As a result, the console displays the following message
Sum of numbers >= 20: 97Overall flow
- First, we prepare a list called
numbers. - From that list, add values greater than 20 to
filteredNumbers. - Calculate the sum of
filteredNumbers. - Output the total to the console.
Execution Result
When the program is executed, the following results are displayed on the console
Sum of numbers >= 20: 97Thus, the problem combines the basic usage of list manipulation, conditional branching, looping, and debugging output.
