线性链表的深拷贝
我有一个由节点组成的线性链表:
class Node{
Object data;
Node link;
public Node(Object pData, Node pLink){
this.data = pData;
this.link = pLink;
}
public String toString(){
if(this.link != null){
return this.data.toString() + this.link.toString();
}else{
return this.data.toString() ;
}
}
public void inc(){
this.data = new Integer((Integer)this.data + 1);
}
public Node copy(){
return new Node(this.data, this.link.copy());
}
}
我想制作列表的深层副本。然后递增原始列表的每个节点并打印两者。不知道代码是否正确。
class Aufg1{
public static void main(String args[]){
Node node3 = new Node(new Integer(3), null);
Node node2 = new Node(new Integer(2), node3);
Node node1 = new Node(new Integer(1), node2);
System.out.println(node1.copy().toString());
System.out.println(node1.toString());
}
}
...第二个 println 只给了我 123,但副本有问题。有什么想法吗?
更新:
public Node copy(){
if(this.link != null){
return new Node(new Integer((Integer)this.data), this.link.copy());
}else{
return new Node(new Integer((Integer)this.data), null);
}
}
I have a linear linked list that consists of nodes:
class Node{
Object data;
Node link;
public Node(Object pData, Node pLink){
this.data = pData;
this.link = pLink;
}
public String toString(){
if(this.link != null){
return this.data.toString() + this.link.toString();
}else{
return this.data.toString() ;
}
}
public void inc(){
this.data = new Integer((Integer)this.data + 1);
}
public Node copy(){
return new Node(this.data, this.link.copy());
}
}
I want to make a deep copy of a list. Then increment each node of the original list and print both. I do not know if the code is right.
class Aufg1{
public static void main(String args[]){
Node node3 = new Node(new Integer(3), null);
Node node2 = new Node(new Integer(2), node3);
Node node1 = new Node(new Integer(1), node2);
System.out.println(node1.copy().toString());
System.out.println(node1.toString());
}
}
...gives me just 123 for the second println but something is wrong with the copy. Any ideas?
Update:
public Node copy(){
if(this.link != null){
return new Node(new Integer((Integer)this.data), this.link.copy());
}else{
return new Node(new Integer((Integer)this.data), null);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
深层复制意味着您还需要复制数据。
因此,您需要决定如何复制该对象。
编辑:您可以使用诸如深度克隆库之类的东西来提供帮助。他们使用反射等。如果您知道自己正在创建对象类的对象类型,则可以创建复制构造函数。
A deep copy would imply that you need to do a copy of data also.
So you need to decide how you are going to copy the object.
EDIT: You can use something like the Deep Cloning Library to help. They use reflection etc. If you know the type of objects in that you are creating the class of objects yourself you can create a copy constructor.
好吧,从 link 可能为 null 的事实来看,您可能希望避免在不先检查的情况下调用 this.link.copy() 。我猜你所说的问题是空指针异常。
编辑:
是的,还有文森特所说的。 :)
请务必复制该对象。
Well, judging from the fact that link could be null, you may wish to avoid calling this.link.copy() without checking first. I am guessing the problem you are talking about is a null pointer exception.
EDIT:
Yeah, and what Vincent said. :)
Be sure to copy the object as well.