In: Computer Science
Why does this not work? (Javascript)
public void addStep(List<String> instructions) {
Iterator<String> iter =
instructions.iterator();
while (iter.hasNext()) {
String step =
iter.next();
if
(instructions.contains(step) == false) {
iter.add(step);
}
}
}
here the problem is Iterator can't have add method so we can use ListIterator to acheive this
public void addStep(List<String> instructions) {
ListIterator<String> iter = instructions.listIterator();
while (iter.hasNext()) {
String step = iter.next();
if (instructions.contains(step) == false) {
iter.add(step);
}
}
}