sou*_*frk 3 java stack-overflow linked-list
只是为了理解实现,操作等,我从JDK复制了LinkedList类,并以非泛型(String only)的方式进行了修改.你可以在这里找到实际的代码.与问题相关,这里是
addLast()
public void addLast(String e){
System.out.println("Invoked:addLast()");
linkLast(e);
}
Run Code Online (Sandbox Code Playgroud)
节点内部类
private static class Node {
String item;
Node next;
Node prev;
Node(Node prev, String element, Node next) {
this.item = element;
this.next = next;
this.prev = prev;
}
@Override public String toString() {
return "Node [item=" + item + ", next=" + next + ", prev=" + prev+ "]";
}
}
Run Code Online (Sandbox Code Playgroud)
linkLast()
void linkLast(String e) {
System.out.println("Invoked:linkLast(" + e + ")");
final Node l = last;
final Node newNode = new Node(l, e, null);
System.out.println("\tCreatd new node as:" + newNode);
last = newNode;
System.out.println("\tSetting this node as last");
if (l == null){
// Applicable for 1st node. Last & First point to newNode
first = newNode;
System.out.println("\tLast was null, setting this node as first.");
}
else{
// Set newNode as next of previous Last.
System.out.println("\tLast was not null, seting this node as next node of previous Last.");
l.next = newNode;
}
size++;
//System.out.println("Size of list:" + size);
modCount++;
System.out.println("\tMod Count:" + modCount + "\n");
}
Run Code Online (Sandbox Code Playgroud)
我得到一个stackoverflow错误,当我执行这个类时,
public static void main(String args[]){
LinkedListDemo obj = new LinkedListDemo();
obj.add("B");
obj.addFirst("A");
obj.addLast("C");
}
Run Code Online (Sandbox Code Playgroud)
产量
Invoked:add(B)
Invoked:linkLast(B)
Creatd new node as:Node [item=B, next=null, prev=null]
Setting this node as last Last was null, setting this node as first.
Mod Count:1
Invoked:addFirst()
Invoked:linkFirst(A)
Creatd new node asNode [item=A, next=Node [item=B, next=null, prev=null], prev=null]
Seting this node as first
First was not null, seting this node as next node of previous First.
Mod Count:2
Invoked:addLast()
Invoked:linkLast(C)
Exception in thread "main" java.lang.StackOverflowError
at java.lang.String.getChars(String.java:826)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:416)at java.lang.StringBuilder.append(StringBuilder.java:132)
at java.lang.StringBuilder.<init>(StringBuilder.java:110)
at collections.LinkedListDemo$Node.toString(LinkedListDemo.java:570)
at java.lang.String.valueOf(String.java:2847)
at java.lang.StringBuilder.append(StringBuilder.java:128)
at collections.LinkedListDemo$Node.toString(LinkedListDemo.java:570)
Run Code Online (Sandbox Code Playgroud)
然后最后3行不断重复.我无法弄清楚造成这种情况的原因.
更新 - 基于收到的回复
所以,如果理解是正确的,
Invoked:add(A)
Invoked:linkLast(A)
Creatd new node as:Node [item=A, next=null, prev=null]
Setting this node as last
Last was null, setting this node as first.
Mod Count:1
Invoked:add(B)
Invoked:linkLast(B)
Creatd new node as:Node [item=B, next=null, prev=Node [item=A, next=null, prev=null]]
Setting this node as last
Last was not null, seting this node as next node of previous Last.
Mod Count:2Run Code Online (Sandbox Code Playgroud)
在,此阶段"节点B"仅使用单向链接(prev)创建,其中调用toString().只有在其他侧链接(下一个)被链接之后.并且这个双链接是下次插入时无限递归的原因.
也许这就是为什么toString()方法既没有在它的两个超类AbstractSequentialList,AbstractList中实现,也没有在AbstractCollection中实现的原因.该实现提取Iterator,迭代打印colletcion.
那么,为什么它只在第3次插入时发生.两次插入不会导致此问题.
问题是你的toString方法,它在尝试打印prev和next节点时递归调用它自己.
如果next打印的当前的Node不为空,next.toString()将被调用,而当prev那Node被打印出来,toString()原来Node被再次调用,所以没有结束递归.
可能的方法:
@Override
public String toString()
{
return "Node [item=" + item + ", next=" + (next==null?"null":next.item) + ", prev=" + (prev==null?"null":prev.item) + "]";
}
Run Code Online (Sandbox Code Playgroud)
关于意见:
添加第一个节点后,列表如下所示:
prev next
null <- Node1 -> null
Run Code Online (Sandbox Code Playgroud)
first并且last都引用Node1
toString中没有无限递归的风险.
添加第二个节点后,列表如下所示:
null <- Node1 -> <- Node2 -> null
Run Code Online (Sandbox Code Playgroud)
first是指Node1并且last指的是Node2
如您所见,Node1和Node2相互引用(Node1是Node2 prev,Node2是Node1 next).因此,尝试toString()在第二次插入后使用原始方法打印其中一个节点将导致无限递归.
但是,由于在l.next = newNode;执行行之前打印新节点,因此在第二次插入时不会获得无限递归,仅在第三次插入中.如果移动System.out.println("\tCreatd new node as:" + newNode);到结尾linkLast,第二次插入将导致无限递归.
| 归档时间: |
|
| 查看次数: |
154 次 |
| 最近记录: |