Counting Calls Before an Early Exit

A Java object named readings implements Iterable for integer sensor readings. Each fresh iterator returns 18, 25, 31, 46 and 52, in that order. The object records every invocation of iterator(), and its returned iterator records every invocation of hasNext() and next().


All three counters begin at zero immediately before this code runs. The methods do not invoke one another internally, and no other code accesses the object or iterator during this execution.


Java
int accepted = 0;
for (int reading : readings) {
    if (reading >= 40) {
        break;
    }
    accepted++;
}


Determine the final numbers of calls to iterator(), hasNext() and next(). Enter each count as an exact integer number of calls.

Your answers

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: