Salesforce関連の記事を書いています。

  1. Apex練習問題

Exercise 5: Filtering and Calculating Lists

Write the code according to the following conditions.

  1. Declare a variable numbers of type List and store the six values 5, 12, 18, 25, 32, 40.
  2. Create a new list filteredNumbers that contains only values where each element in the list is greater than or equal to 20.
  3. Calculate the sum of the filteredNumbers and output the value to the console using System.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, where Integer is a data type that handles integers and List 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 called num, 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.
  • if statement: The conditional statement if (num >= 20) checks if num 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 to filteredNumbers.
  • filteredNumbers.add(num) adds the number that satisfies the condition (20 or more) to filteredNumbers.

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 to sum.
    • In the first loop, num is 25, so we add 25 to sum. sum becomes 0 25 = 25.
    • In the second loop, num is 32, so sum is 32. sum is 25 32 = 57.
    • In the third loop, num is 40, so sum is 40. sum is 57 40 = 97.

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

  1. First, we prepare a list called numbers.
  2. From that list, add values greater than 20 to filteredNumbers.
  3. Calculate the sum of filteredNumbers.
  4. 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.

Apex練習問題 recent post

  1. Exercise 3: Iteration and List Manipulation

  2. Exercise 1: Variable Declaration and Manipula…

  3. Exercise 2: Conditional Branching and Variabl…

  4. Exercise 4: Combining conditional branches an…

  5. Exercise 5: Filtering and Calculating Lists

関連記事

PAGE TOP