#315 Iterator for Nested String Iterators
Implement the following class:
public class SuperIterator implements Iterator<String> {
public SuperIterator(Object... iterators) {
...
}
}
The constructor arguments may be a String, an Iterator<String>, or an Iterator<Iterator<String>>, generalized to iterators of any nesting depth. Example:
var it0 = Collections.<String>emptyList().iterator();
var it1 = Arrays.asList("1", "2", "3").iterator();
String it2 = "4";
var it3 = Arrays.asList(
Arrays.asList("5").iterator(),
Arrays.asList("6", "7").iterator()).iterator();
var it = new SuperIterator(it1, it2, it0, it3);
while (it.hasNext()) {
System.out.println(it.next());
}
It must print 1, 2, 3, 4, 5, 6, 7, each on a new line.
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor