From the course: Programming Foundations: Design Patterns

Unlock the full course today

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

Using built-in iterators

Using built-in iterators

From the course: Programming Foundations: Design Patterns

Start my 1-month free trial

Using built-in iterators

- [Instructor] Java offers a built-in iterator interface. Java.util.iterator. We can use this interface instead of building our own if we want to. The java.util.iterator interface acts both as an interface that your own iterator classes can implement as well as the type of the Java collection classes built-in iterators. Classes like ArrayList, Vector and LinkedList all have an iterator method that returns a ready built iterator with a type java.util.iterator. Now if you're asking, well, why did we build our own iterator for the diner menu? Well, we wanted you to see how to create an iterator from scratch, but now that you know how to do that, you should also know how to use the built-in iterator interface and the collection iterators. It's worth noting that Java arrays don't have built-in iterators in the same way the collection classes do. So building an iterator for an array or custom collection is a good way to see how the iterator pattern works. In our cafe example, the Pancake…

Contents