Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private class LinkedListIterator implements java.util.Iterator<E>{
- private Node<E> current = first;
- private Node<E> prev = null; // ha koll på prev bara för remove!
- private int expectedModCount = modCount;
- int position = 0; // Representerar current index
- @Override
- public boolean hasNext() {
- if(current==null) {
- return false;
- } else {
- return true;
- }
- }
- @Override
- public E next() {
- if(modCount!=expectedModCount) {
- throw new java.util.ConcurrentModificationException();
- }
- if(!hasNext()) {
- throw new java.util.NoSuchElementException();
- }
- E nextItem = current.element;
- prev = current;
- current = current.next;
- position++;
- return nextItem;
- }
- @Override
- public void remove() {
- if(position == 0)
- throw new IllegalStateException();
- if (prev != null){
- MyALDAList.this.remove(prev.element);
- position--;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement