Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package prep_29_iteratorpattern;
- public class DinerMenuIterator implements Iterator {
- MenuItem[] items;
- int position = 0; // current position of the iteration over the array
- public DinerMenuIterator(MenuItem[] items) {
- this.items = items; // the items we're going to iterate over
- }
- public Object next() { // returns next item in array, increments position
- MenuItem menuItem = items[position];
- position = position + 1;
- return menuItem;
- }
- public boolean hasNext() {
- if (position >= items.length || items[position] == null) {
- return false;
- } else {
- return true; // return true if there are more elements we haven't looked at
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment