简体   繁体   English

Java-自定义迭代器无法跟踪自定义链接列表的头部

[英]Java - Custom iterator not able to track head of custom linked list

The classes are not complete, but here's what I have so far and I expected the test below to pass. 这些课程还没有完成,但是到目前为止,这是我想要的,我希望下面的测试能够通过。

public class LinkedList<T> extends AbstractSequentialList<T> {
  private Node<T> head;

  @Override
  public boolean add(T element) {
    if(head == null) {
      head = new Node(element);
    }
    return true;
  }

  @Override
  public ListIterator<T> listIterator(int index) {
    return new LinkedListIterator<>();
  }

  @Override
  public int size() {
    return 0;
  }

  private class LinkedListIterator<T> implements ListIterator<T> {
    private Node<T> current;

    public LinkedListIterator() {
        current = (Node<T>) head;
    }

    @Override
    public boolean hasNext() {
        return (current != null && current.getNext() != null)? true : false;
    }

    @Override
    public T next() {
        return null;
    }
  }
}

Here is the Node class. 这是Node类。

public class Node<T> {

  private T value;
  private Node next;

  public Node(T value) {
    this.value = value;
  }

  public Node(T value, Node next) {
    this.value = value;
    this.next = next;
  }

  public T getValue() {
    return value;
  }

  public Node getNext() {
      return next;
  }

  public void setNext(Node next) {
      this.next = next;
  }
}

My iterator test is like this. 我的迭代器测试就是这样。

LinkedList<String> list;
ListIterator<String> iterator;

@Before
public void setUp() throws Exception {
    list = new LinkedList<>();
    iterator = list.listIterator();
}

@Test
public void testHasNext() throws Exception {
    assertThat(iterator.hasNext(), is(false));

    list.add("Hello World");
    assertThat(iterator.hasNext(), is(true));
}

However, I'm failing on the second assertion. 但是,我在第二个断言上失败了。 My issue is that the "current" pointer in the iterator is always null even though I'm setting it to the head of the enclosing LinkedList class. 我的问题是,即使将迭代器中的“当前”指针设置为封闭的LinkedList类的开头,它也始终为null。 How can I fix this? 我怎样才能解决这个问题? Thanks. 谢谢。

It looks the value of current is set inside the constructor of LinkedListIterator. 看起来current的值是在LinkedListIterator的构造函数中设置的。

It hasn't been updated after you have added an element to the list. 将元素添加到列表后,它尚未更新。 This seems to your problem here. 这似乎是您的问题。

What is wrong is your test, IMO. IMO,您的测试有问题。

You shouldn't expect the iterator to point to the first element if the first element has been added after the iterator has been constructed. 如果在构造迭代器之后添加了第一个元素,则不应期望迭代器指向第一个元素。

Now, why does your iterator work this way? 现在,为什么您的迭代器以这种方式工作? Because Java is pass-by-value. 因为Java是按值传递的。 When you construct an iterator, the iterator receives a copy of the reference to the first node of the list. 构造迭代器时,迭代器会收到对列表第一个节点的引用的副本。 And at this time, this reference is null, because you haven't added any node yet. 目前,此引用为null,因为尚未添加任何节点。

If you really want the iterator to "see" the first node of the list even after it has been constructed, then the iterator needs to get the first node of the list in hasNext() , not in the constructor. 如果您确实希望迭代器即使在构造列表之后仍“看到”列表的第一个节点,则迭代器需要在hasNext()中而不是在构造函数中获取列表的第一个节点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM