Rewrite the following piece of java code using iterator pattern. And change the operation inside the for loop to another operation of your own. And What was the advantage of using the iterator ? import java.util.Iterator
Rewrite the following piece of java code using iterator pattern. And change the operation inside the for loop to another operation of your own. And What was the advantage of using the iterator ?
import java.util.Iterator
String[ ] originalData = { "one", "two", "three", "four", "five" };
List<String> strings = new ArrayList<>(Arrays.asList(originalData));
for ( int i=0; i<strings.size(); i++) {
// process strings.get(i): here, just print
System.out.println(strings.get(i));
}
Create an Iterator instance for the given list and then we can iterate over the iterator and print the each element as long as there are elements in iterator
Iterators are useful when compared to loops as loops doesn't allows the update operations on collection whereas Iterator supports this
Step by step
Solved in 3 steps with 1 images