在toString方法中返回动态堆栈linkedlist的所有值



我正试图弄清楚如何输出节点的所有版本"最新的";使用toString方法,但我对如何在这种情况下使while循环正确工作感到困惑。我想知道是否有一种方法可以做到这一点

以下是toString()方法:

public String toString() {
String output = "key: " + key ;
Node<VersionedObject> currNode = latest;

while (latest != null){
output += latest+ "nt";
latest.setNext(latest);
} // This isn't particularly working

//return "key: " + key +"nt" +latest + "nt" + latest.getNext(); // placeholder but it's getting closer. This one is INSANELY specific
return output;
}

,这是创建列表的方法:

public StackOfVersionedObjects(String key, final Object object) {
this.key = key;
VersionedObject vrObject = new VersionedObject(object);
latest = new Node<VersionedObject>(vrObject);
}

,这是我的节点类:

public class Node<T> {
private T    data;
private Node next;
public Node() {
data = null;
next = null;
}
public Node(T data) {
this.data = data;
next      = null;
}
public T getData() {
return data;
}
public Node<T> getNext() {
return next;
}
public void setData(T data) {
this.data = data;
}
public void setNext(Node<T> next) {
this.next = next;
}
@Override
public String toString() {
return data.toString();
}
}

latest更改为currentNode并像这样循环

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("key: " + key);
Node<VersionedObject> currNode = latest;

while (currNode != null){
builder.append(currNode + "nt");
currNode = currNode.getNext();
}
return builder.toString();
}

相关内容

  • 没有找到相关文章

最新更新