我有2個關於鏈表的問題,所以我想我會發布他們在一個問題。java鏈接列表複製構造函數和字符串構造函數
首先我會從字符串
// constructor from a String
public CharList(String s)
{
head = head.setCharacter(s);
}
顯示我的節點類和拷貝構造函數和構造函數從字符串
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();
}
}
構造,當我嘗試編譯我得到一個錯誤,我的複製構造函數它說,它不能找到符號this.setCharacter()
...和l.setCharacter()
...
我只是在做它完全錯誤?
並與我的構造函數從字符串我知道這是錯的。我想過使用charAt()
,但我怎麼知道什麼時候停止循環來做到這一點?這是一個很好的方法嗎?
任何幫助,將不勝感激。
您收到答案的7個問題至今並沒有接受任何的答案。你會發現,如果你不接受好的答案,人們不太願意幫忙。 –
你必須讀一點關於封裝的概念,如果你自己解決編譯問題 – Genjuro
你必須接受一個答案? – alexthefourth