Question
“The constructor MyIterable should accept parameter of type List<String>.”
I am unable to complete the “Get Hands-On with an Iterable Variable in For Loops” Trailhead module because I keep getting the following error message:
Here is the code I have for my MyIterable
class, where its constructor is supposed to accept a List<String>
parameter:
public class MyIterable implements Iterable<String> {
private List<String> strings;
public MyIterable(List<String> strList) {
strings = strList;
strings.iterator();
}
public Iterator<String> iterator() {
return strings.iterator();
}
}
I am following the instructions, but I am still facing the error. How can I fix this?
Solution
The error arises because the constructor is not being recognized properly due to an unnecessary call to strings.iterator()
within the constructor. The issue can be resolved by ensuring that the constructor only accepts the List<String>
and stores it without calling the iterator method inside the constructor.
Here is the corrected version of the MyIterable
class with the right constructor:
public class MyIterable implements Iterable<String> {
private List<String> strings;
public MyIterable(List<String> strings) {
this.strings = strings;
}
public Iterator<String> iterator() {
return strings.iterator();
}
}
In this version, the constructor correctly accepts a List<String>
and assigns it to the strings
field. The iterator()
method then returns the iterator for that list when needed. This ensures that the class conforms to the Iterable<String>
interface without unnecessary calls inside the constructor.
Test Class:
To validate the functionality of your iterable class, you can use the following test class:
@IsTest
public class MyIterableTest {
@IsTest
static void testIterableForLoop() {
List<String> strings = new List<String>{'Hello', 'World'};
MyIterable iterable = new MyIterable(strings);
for (String str : iterable) {
System.debug(str);
}
}
}
Explanation of the Code:
- In the first code snippet, the
MyIterable
class implements theIterable<String>
interface, meaning it must define aniterator()
method that returns anIterator<String>
. The constructor accepts aList<String>
and assigns it to thestrings
field. - The
iterator()
method simply returns the iterator for thestrings
list, enabling it to be iterated using a for-each loop. The constructor doesn’t need to callstrings.iterator()
inside itself, as it’s handled by the iterator method. - The second code snippet is a test class
MyIterableTest
, marked with@IsTest
to make it a test class. It creates aList<String>
, passes it to theMyIterable
constructor, and iterates over the list in a for-each loop, verifying that theiterator()
method works as expected. - The test class checks if the
MyIterable
class functions correctly by printing the values of the list ('Hello'
and'World'
) to the debug log, ensuring the class is correctly handling the iteration and implementing theIterable
interface as required.
Summing Up
The error occurs because the constructor in the MyIterable
class is incorrectly calling the strings.iterator()
method inside itself. To resolve this, the constructor should simply accept a List<String>
and assign it to the strings
field without invoking the iterator method.
The MyIterable
class implements the Iterable<String>
interface, which requires an iterator()
method that returns an iterator for the strings
list. This allows the class to be used in a for-each loop to iterate over the list of strings.
A test class can be used to validate that the MyIterable
class functions as expected. It creates a list, passes it to the class, and checks if the iteration works by printing the values to the debug log. This ensures that the MyIterable
class can be used correctly for iteration.
Job-Oriented Salesforce Course: Enroll for Free Demo
Our Salesforce Course is tailored to give you a deep, hands-on understanding of the Salesforce platform, empowering you to excel in the dynamic world of CRM solutions. We cover all essential modules, including Salesforce Admin, Developer, and AI, with a focus on practical learning. Through real-time project work and scenario-based assignments, you’ll develop the skills to handle real-world business challenges efficiently. Led by industry experts, our program ensures you gain both the knowledge and the confidence needed to thrive in the Salesforce ecosystem.
Along with technical training, our Salesforce training in Bangalore offers personalized mentorship, certification guidance, and interview preparation to help you stand out in the competitive job market. You’ll benefit from comprehensive study materials, live project experiences, and one-on-one support to enhance your expertise. We’re committed to ensuring that by the end of the course, you’ll be fully equipped to tackle Salesforce roles with practical, in-demand skills that employers are actively seeking. Start your Salesforce journey with us and unlock new career opportunities today!