gpt4 book ai didi

java链表复制构造函数和字符串构造函数

转载 作者:行者123 更新时间:2023-12-01 15:42:41 26 4
gpt4 key购买 nike

我有 2 个关于链接列表的问题,所以我想我会将它们发布在一个问题中。

首先,我将显示我的节点类以及复制构造函数和字符串构造函数

class CharNode 
{

private char letter;
private CharNode next;

public CharNode(char ch, CharNode link)
{
letter = ch;
next = link;
}

public void setCharacter(char ch)
{
letter = ch;
}

public char getCharacter()
{
return letter;
}

public void setNext(CharNode next)
{
this.next = next;
}

public CharNode getNext()
{
return next;
}

}

复制构造函数

   // copy constructor  
public CharList(CharList l)
{
CharNode pt = head;

while(pt.getNext() != null)
{
this.setCharacter() = l.getCharacter();
this.setNext() = l.getNext();
}
}

字符串的构造函数

   // constructor from a String 
public CharList(String s)
{
head = head.setCharacter(s);


}

当我尝试编译时,我的复制构造函数出现错误,它说它找不到符号 this.setCharacter()... 和 l.setCharacter()...

我这样做完全错了吗?

使用我的字符串构造函数,我知道那是错误的。我考虑过使用 charAt() 但我怎么知道何时停止循环呢?这是一个好的方法吗?

如有任何帮助,我们将不胜感激。

最佳答案

在 CharList 构造函数中,this 引用 CharList 类,该类没有 setCharacter() 方法(CharNode 有) 。另外,当你调用java中的方法时,你需要传递参数,例如setFoo(newFoo),而不是setFoo() = newFoo

关于java链表复制构造函数和字符串构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7774581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com