Repairing Nested Traversal

A scheduling program should count every ordered pair of four session codes, including pairs that use the same code twice. Its target is all 16 ordered pairs. The unchanged list and current Java code are:


Java
var sessions = List.of("P", "Q", "R", "S");
var outer = sessions.iterator();
var inner = sessions.iterator();
int pairs = 0;
while (outer.hasNext()) {
    outer.next();
    while (inner.hasNext()) {
        inner.next();
        pairs++;
    }
}


Which changes, applied separately to this code, produce the intended count? Select every correct change.

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Iterable and Iterator Questions: