I am unable to complete this Trailhead Module: Get Hands-On with an Iterable Variable in For Loops, because I keep getting this error message: "The constructor MyIterable should accept parameter of type List<String>."
Here's my MyIterable Class with its constructor accepting the correct type of 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(); }}
Hi @Edward Dalton,
- You need to assign strings (In constructor, It should be strings not strList) to strings class variable. Use this keyword.
- You should replace strings = strList; with this.strings = strings;
- In constructor remove strings.iterator();
Code should be like this -
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();
}
}