LoganBlackisle

DinerMenuIterator

Jun 14th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. package prep_29_iteratorpattern;
  2.  
  3. public class DinerMenuIterator implements Iterator {
  4. MenuItem[] items;
  5. int position = 0; // current position of the iteration over the array
  6.  
  7. public DinerMenuIterator(MenuItem[] items) {
  8. this.items = items; // the items we're going to iterate over
  9. }
  10.  
  11. public Object next() { // returns next item in array, increments position
  12. MenuItem menuItem = items[position];
  13. position = position + 1;
  14. return menuItem;
  15. }
  16.  
  17. public boolean hasNext() {
  18. if (position >= items.length || items[position] == null) {
  19. return false;
  20. } else {
  21. return true; // return true if there are more elements we haven't looked at
  22. }
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment