[英]Java custom iterator for linked list
for uni we are supposed to implement an iterator for a String linked list by ourselves.对于 uni,我们应该自己为 String 链表实现迭代器。 But the infos how to do that was pretty small.
但是关于如何做到这一点的信息非常少。 So we tried it by ourselves and googled a lot but all the explanations we found dont include the whole code and I dont get how to implement the iterator correct.
所以我们自己尝试了它并用谷歌搜索了很多,但我们发现的所有解释都没有包含整个代码,我不知道如何正确实现迭代器。 I think everything works fine as long as we use the for each loop to use the iterator but as soon as we are trying to use the "while (iterator.hasnext) { next }" thing it stays in the first element of the linked list.
我认为只要我们使用 for each 循环来使用迭代器,一切都可以正常工作,但是一旦我们尝试使用“while (iterator.hasnext) { next }”,它就会停留在链表的第一个元素中. I think I know this problem is based on that we are always instantiating a new iterator but I dont get how to implement it else.
我想我知道这个问题是基于我们总是实例化一个新的迭代器,但我不知道如何实现它。 Hopefully someone can help, I really don't know what to do, I tried everything..
希望有人可以提供帮助,我真的不知道该怎么办,我尝试了一切..
import java.util.Iterator;
import java.util.NoSuchElementException;
public class LinkedList implements Iterable<String> {
// ---------- Attributes ----------
private int size = 0;
private Node head = null;
// private Iterator<String> linkedListIterator = this.iterator(); // ??
static class Node {
// ---------- Attributes ----------
private String object;
private Node next;
// ---------- Constructors ----------
public Node(String object, Node node) {
this.object = object;
this.next = node;
}
public Node() {
this(null, null);
}
// ---------- Getter, Setter ----------
public String getElement() {
return this.object;
}
public void setElement(String object) {
this.object = object;
}
public Node getNext() {
return this.next;
}
public void setNext(Node node) {
this.next = node;
}
}
class LinkedListIterator implements Iterator<String> {
// ---------- Attributes ----------
private Node currentNode = null;
private int counter = 0;
// ---------- Constructor ----------
public LinkedListIterator(LinkedList linkedList) {
this.currentNode = linkedList.head;
}
// ---------- Getter, Setter, Methods ----------
public boolean hasNext() {
return this.currentNode != null;
}
public String next() {
if (!this.hasNext()) {
System.out.println("Fehler: ");
throw new NoSuchElementException();
}
String object = this.currentNode.getElement(); // ?
this.currentNode = this.currentNode.getNext();
this.counter++;
return object;
}
public int getCounter() {
return this.counter;
}
}
// ---------- Getter, Setter, Methods ----------
public Node getHead() {
return this.head;
}
public void addFirst(String object) {
// new node as head
Node newNode = new Node(object, this.head);
this.head = newNode;
this.size++;
}
public String getFirst() { //throws ListEmptyException {
if (isEmpty()) {
// throw new ListEmptyException();
}
return this.head.getElement();
}
public String removeFirst() { //throws ListEmptyException {
if (isEmpty()) {
// throw new ListEmptyException();
}
String object = this.head.getElement();
this.head = this.head.getNext();
return object;
}
public boolean isEmpty() {
return this.head == null;
}
public int getSize() {
return this.size;
}
@Override
public Iterator<String> iterator() {
System.out.println("helo");
return new LinkedListIterator(this);
}
public String toString() {
String output = "";
// this is working:
// for (String element: this) {
// output += element + "\n";
// }
while (this.iterator().hasNext()) {
System.out.println(this.iterator().hasNext());
output += this.iterator().next() + "\n";
}
return output;
}
public static void main(String[] args) {
LinkedList ll = new LinkedList();
ll.addFirst("a");
ll.addFirst("b");
ll.addFirst("c");
ll.addFirst("d");
ll.addFirst("e");
System.out.println(ll.toString());
}
}
Problem solved by this问题由此解决
But new question: Why is this working但新问题:为什么这有效
public String toString() {
String output = "";
Iterator<String> iterator = this.iterator();
while (iterator.hasNext()) {
output += it.next() + "\n";
}
return output;
}
But this not但这不是
public class LinkedList implements Iterable<String> {
private Iterator<String> linkedListIterator = this.iterator();
public String toString() {
String output = "";
while (this.linkedListIterator.hasNext()) {
output += this.linkedListIterator.next() + "\n";
}
return output;
}
}
Your implementation of LinkedListIterator is correct, the problem is in the toString() method.您对 LinkedListIterator 的实现是正确的,问题出在 toString() 方法中。 You are calling this.iterator() 3 times, so each time you return a new instance of LinkedListIterator.
您调用 this.iterator() 3 次,因此每次都返回 LinkedListIterator 的一个新实例。 Instead you have to call this.interator() only once and use the instance you get.
相反,您只需调用 this.interator() 一次并使用您获得的实例。 Like this:
像这样:
Iterator<String> it=this.iterator();
while (it.hasNext()) {
System.out.println(it.hasNext());
output += it.next() + "\n";
}
Regarding the new question.关于新问题。
If you instantiate the private Iterator<String> linkedListIterator
attribute in the body of the class, (Something that should never be done), every time you refer to it you will make a call to the public Iterator<String> iterator()
method and you will get a new instance of LinkedListIterator
.如果你在类的主体中实例化
private Iterator<String> linkedListIterator
属性,(一些永远不应该做的事情),每次你引用它时你都会调用public Iterator<String> iterator()
方法和您将获得LinkedListIterator
的新实例。
You are making the same mistake as in the beginning.你犯了和一开始一样的错误。 This is an example of why attributes should be instantiated only within a method declaration.
这是一个为什么属性应该只在方法声明中实例化的例子。
Remember that an iterator can only move forward, if you want to restart it you must create a new instance.请记住,迭代器只能向前移动,如果要重新启动它,则必须创建一个新实例。 That's what you do by calling
this.iterator()
.这就是你通过调用
this.iterator()
所做的。
I recommend you to use some debugging tools so you can see the instructions that are executed建议你使用一些调试工具,这样你就可以看到执行的指令
Also, there is a design pattern that deals iterators.此外,还有一种处理迭代器的设计模式。 https://en.wikipedia.org/wiki/Iterator_pattern
https://en.wikipedia.org/wiki/Iterator_pattern
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.