Advertisement
xickoh

Untitled

Nov 6th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package Collection;
  7.  
  8. import java.util.Iterator;
  9. import java.util.NoSuchElementException;
  10.  
  11. /**
  12. * @author 8150121 e 8150133
  13. * @param <T> tipo genérico
  14. */
  15. public class ArrayIterator<T> implements Iterator<T> {
  16.  
  17. private final int count;
  18. private int current;
  19. private final T[] items;
  20.  
  21. public ArrayIterator(T[] items, int count) {
  22. this.items = items;
  23. this.count = count;
  24. this.current = 0;
  25. }
  26.  
  27. /**
  28. *
  29. * @return true se existe mais que um elemento do array para retornar
  30. */
  31. @Override
  32. public boolean hasNext() {
  33. return (this.current < this.count);
  34. }
  35.  
  36. /**
  37. * Retorna o atual elemento do array, e move o cursor para o próximo
  38. *
  39. * @return o atual elemento do array
  40. */
  41. @Override
  42. public T next() {
  43. if (!hasNext()) {
  44. throw new NoSuchElementException();
  45. }
  46.  
  47. this.current++;
  48.  
  49. return this.items[this.current - 1];
  50. }
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement