From the course: Java Design Patterns: Behavioral Part 1

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Solution: The Iterator pattern

Solution: The Iterator pattern - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 1

Start my 1-month free trial

Solution: The Iterator pattern

(upbeat music) - [Narrator] Now let's have a look at my solution to the exercise. As usual, if yours is a bit different to mine, that's totally fine. So first of all, let's have a look at the stafflist class. This now implements the iterable interface. I also have a field called employees, which is an array of employee objects. And I'm setting this to the employees which I'm passing into the constructor. I have a method called getemployees to return the array of employees. And I have an iterator method, which returns a new stafflistiterator. So let's have a look at the stafflistiterator class next. In here, I have two fields, a stafflist and an intcode index. And I'm passing in the stafflist and the constructor and assigning that to the stafflist field and I'm also setting index to zero. Now let's have a look at the hasnext method. I'm creating an array of employees by calling the getemployees method on the…

Contents