Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. private class LinkedListIterator implements java.util.Iterator<E>{
  2. private Node<E> current = first;
  3. private Node<E> prev = null; // ha koll på prev bara för remove!
  4. private int expectedModCount = modCount;
  5. int position = 0; // Representerar current index
  6.  
  7. @Override
  8. public boolean hasNext() {
  9.  
  10. if(current==null) {
  11. return false;
  12. } else {
  13. return true;
  14. }
  15. }
  16.  
  17. @Override
  18. public E next() {
  19. if(modCount!=expectedModCount) {
  20. throw new java.util.ConcurrentModificationException();
  21. }
  22.  
  23. if(!hasNext()) {
  24. throw new java.util.NoSuchElementException();
  25. }
  26.  
  27. E nextItem = current.element;
  28. prev = current;
  29. current = current.next;
  30. position++;
  31.  
  32. return nextItem;
  33. }
  34.  
  35. @Override
  36. public void remove() {
  37. if(position == 0)
  38. throw new IllegalStateException();
  39. if (prev != null){
  40. MyALDAList.this.remove(prev.element);
  41. position--;
  42. }
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement