java链表复制构造函数
我在链接列表的构造函数方面遇到问题。它需要一个字符串,并应该为每个字符创建一个节点。
每次我尝试打印列表时都会遇到空指针异常。这是否意味着甚至没有创建第一个节点?
下面是我的节点类和列表构造函数。
class CharNode {
private char letter;
private CharNode next;
public CharNode(char ch, CharNode link)
{
ch = letter;
link = next;
}
public void setCharacter(char ch)
{
ch = letter;
}
public char getCharacter()
{
return letter;
}
public void setNext(CharNode next)
{
this.next = next;
}
public CharNode getNext()
{
return next;
}
}
这是我的构造函数,
// constructor from a String
public CharList(String s) {
CharNode newNode = head;
for(int i = 0; i <s.length(); i++)
{
newNode = new CharNode(s.charAt(i), null);
newNode.setNext(newNode);
}
}
我构建它正确吗?
Im having trouble with a constructor for a linked list. it takes a string and is supposed to create a node for every character.
i get a nullpointerexception everytime i try to print out the list. Does that mean that not even the first node is being created?
below is my node class and the list constructor.
class CharNode {
private char letter;
private CharNode next;
public CharNode(char ch, CharNode link)
{
ch = letter;
link = next;
}
public void setCharacter(char ch)
{
ch = letter;
}
public char getCharacter()
{
return letter;
}
public void setNext(CharNode next)
{
this.next = next;
}
public CharNode getNext()
{
return next;
}
}
and this is my constructor
// constructor from a String
public CharList(String s) {
CharNode newNode = head;
for(int i = 0; i <s.length(); i++)
{
newNode = new CharNode(s.charAt(i), null);
newNode.setNext(newNode);
}
}
am i constructing it correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,你的属性调换了!
你的设置器中应该是
同样的东西。
当 Java 中的方法具有以下形式时:
通常就是您想要的。你必须将你的论点分配给你的班级成员,而不是相反。
另外,当您调用构造函数时,您将:
这使得您的“链接”始终指向自身!想想你需要做什么才能使前一个 Node 指向你刚刚创建的节点(也许以某种方式保存它?)!
我说得够清楚吗?如果我可以进一步解释,请告诉我。
First of all, your attributions are switched around!
should be
Same thing in your setter.
When you have a method in Java of the form:
Is usually what you want. You must assign your argument to your class member, not the other way around.
Also, when you're invoking your constructor, you have:
This make it so that your "link" is always pointing to itself! Think about what you need to do to make the previous Node point to the node you've just created (perhaps save it somehow?)!
Was I clear enough? Let me know if I can explain further.
正如 pcalcao 所说,
=
将右侧的值分配给左侧的变量。您需要将ch = letter;
更改为letter = ch;
并将link = next;
更改为next = link;< /code>
现在,
CharNode newNode = head;
行没有任何意义,除非您在给出的代码之前指定了head
,但它看起来不像。请记住,创建链表时,您不会从任何东西开始,因此即使是像head
这样的“特殊”节点也必须被创建(如果您愿意,可以实例化)。这个想法是创建第一个节点(头),然后将head
分配给该第一个节点。对于第一个节点之后的每个节点,不需要此步骤,因为您只需添加到列表的末尾即可。最后,在构建过程中,您需要引用新节点(就像现在一样)和前一个节点,以便进行正确的附加。您现在的代码只是将列表中的下一个节点设置为当前节点,这意味着一旦创建新的
newNode
,您将丢失对前一个newNode
的引用。刚开始接触链表的一个好方法是一步步画出你想要做的事情,然后尝试将其翻译成代码。希望这会有所帮助。
As pcalcao said,
=
assigns the value on the right to the variable on the left. You'll need to changech = letter;
toletter = ch;
andlink = next;
tonext = link;
Now, the line
CharNode newNode = head;
doesn't mean anything unless you've specified whathead
is prior to the code you've given, but it doesn't look like it. Remember that when creating a linked list, you don't start with anything, so even "special" nodes likehead
must be created (instantiated, if you prefer). The idea is to create the first node (the head), and then assignhead
to that first node. For every node after the first one, this step isn't required, as you simply are adding to the end of the list.Finally, during construction, you'll need a reference to both a new node (like you have now), and the previous node, in order to do proper appending. Your code now is simply setting the next node in the list to the current node, which means that once you create a new
newNode
, you will lose the reference to the previousnewNode
.A good way to approach linked lists when one is first getting started is to draw out what you want to do step by step, then try and translate that into code. Hopefully this is helpful.