目次
Write the code according to the following conditions.
- Declare a variable
numbers of
typeList
and store the six values5
,12
,18
,25
,32
,40
.- Create a new list
filteredNumbers
that contains only values where each element in the list is greater than or equal to20
.- Calculate the sum of the
filteredNumbers
and 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};
List
represents a list of integers, whereInteger is a
data type that handles integers andList is
a data type that can handle multiple values combined into one.numbers
is 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
numbers
are 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,
num
is set to 5. - In the second loop,
num
becomes 12. - In the third loop,
num
becomes 18. - …and so on.
- In the first loop,
- if statement: The conditional statement
if (num >= 20)
checks ifnum
is greater than or equal to 20.- For example, in the first loop,
num
is 5, so this condition is not satisfied. Therefore, nothing is done. - In the fourth loop,
num
is 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,
num
is 25, so we add 25 tosum
.sum
becomes 0 25 = 25. - In the second loop,
num
is 32, sosum
is 32.sum
is 25 32 = 57. - In the third loop,
num
is 40, sosum
is 40.sum
is 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.sum
appends the sum (97) to the string and displays it together.
As a result, the console displays the following message
Sum of numbers >= 20: 97
Overall 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: 97
Thus, the problem combines the basic usage of list manipulation, conditional branching, looping, and debugging output.