Skip to main content
Besuchen Sie uns auf der TDX in San Francisco oder bei Salesforce+ vom 5. bis 6. März, um die Entwicklerkonferenz für die Ära der AI-Agenten zu erleben. Jetzt registrieren.

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

Praktische Aufgabe

+500 Punkte

Machen Sie sich bereit

Sie führen diese(n/s) Lektion in Ihrer eigenen, persönlichen Übungs-Organisationen durch. Klicken Sie auf Launch (Starten), um loszulegen, oder auf den Namen Ihrer Organisation, um eine andere auszuwählen.

Ihre Aufgabe

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
    Teilen Sie Ihr Trailhead-Feedback über die Salesforce-Hilfe.

    Wir würden uns sehr freuen, von Ihren Erfahrungen mit Trailhead zu hören: Sie können jetzt jederzeit über die Salesforce-Hilfe auf das neue Feedback-Formular zugreifen.

    Weitere Infos Weiter zu "Feedback teilen"