Skip to main content
欢迎参加 3 月 5 日至 6 日在旧金山举行的 TDX AI 代理时代开发者大会,或通过 Salesforce+ 参与。立即注册

Get Hands-On with an Iterable Variable in For Loops

Learning Objectives

After completing this unit, you’ll be able to:

  • Use an iterable variable in a for loop.
  • Implement an Iterable Interface in Apex.

Iterate Within For Loops More Easily with Iterable Variable

You can now easily iterate through lists or sets using an Iterable variable in a for loop. Instead of writing complex loops, developers can use this feature to simplify working with for loops that repeat a block of code in each item in a collection. Standardize how you loop through lists and sets to make your code more consistent.

Implement and Test an Iterable Interface in Apex

In this hands-on challenge, you create a custom Apex class that implements the Iterable. You then use this class to iterate over a collection of items within a for loop. This exercise explains how to simplify iteration in Apex using the Iterable interface.

It’s time to complete the hands-on challenge to maintain your Platform Developer I certification.

Resources

实践挑战

+500 分

准备好

您将在您自己的实践组织中完成此单元。单击启动以开始,或单击您的组织的名称以选择不同的组织。

您的挑战

Implement and Test Iteration Using the Iterable Interface in Apex
In this hands-on challenge, you will create a custom Apex class that implements the Iterable. You will then use this class to iterate over a collection of items within a for loop. This exercise will help you understand how to simplify iteration in Apex using the Iterable interface.

Prework:
  • Ensure you have access to a Salesforce Developer Org or a Trailhead Playground.
  • Familiarize yourself with basic Apex programming concepts.
  • Review the Salesforce documentation on the Iterable interfaces.
    • Create a custom Apex class:
      • Name: MyIterable
        • Implements: Iterable<String>
        • Class variable: private List<String> strings
    • Create a constructor for MyIterable:
      • Argument: List<String> strings
      • Initializes the class variable strings from the constructor’s argument strings
    • Implement the iterator method:
      • Return type: Iterator<String>
      • Returns an iterator for the strings list strings.iterator();
    • Create a test class:
      • Name: MyIterableTest
      • Annotation: @IsTest
    • Add a test method to the test class:
      • Name: testIterableForLoop
      • Type: void()
        • Annotation: @IsTest
    • Steps:
      • Create a list of strings: List<String> strings
      • Values: ‘Hello’,’World’
      • Create an instance of MyIterable with the list of strings.
      • Use a for loop to iterate over the MyIterable instance.
      • Print each string using System.debug.
    • Run the test class:
      • Open the Developer Console
      • Navigate to the Test menu
      • Select New Run
      • Select the MyIterableTest class and run the test
    • Verify the Output
      • Check the debug logs to verify that the for loop iterated over the collection and printed each string.
    • Expected Output:
      • The debug logs should display the following output:
        • DEBUG | Hello
        • DEBUG | World
    在 Salesforce 帮助中分享 Trailhead 反馈

    我们很想听听您使用 Trailhead 的经验——您现在可以随时从 Salesforce 帮助网站访问新的反馈表单。

    了解更多 继续分享反馈