我正在尝试StringBuilder
使用链表创建一个类似的类,我想我在构造函数的某个地方搞砸了。任何人都可以在这里找到问题吗?我相信问题在于我如何移动到下一个节点。
节点类:
private class CNode
{
private char data;
private CNode next;
private CNode(char c)
{
data = c;
next = null;
}
private CNode(char c, CNode nextNode)
{
data = c;
next = nextNode;
}
}
构造函数:
private CNode firstNode;
private int length;
public MyString(String s)
{
if(s == null)
{
this.length = 0;
this.firstNode = null;
}
else if(s.length() == 1)
{
this.length = 1;
this.firstNode.data = s.charAt(0);
this.firstNode.next = null;
}
else
{
this.length = s.length();
CNode node = null;
CNode nextNode = null;
this.firstNode = new CNode(s.charAt(0), node);
for(int i = 1; i < s.length(); i++)
{
node = new CNode(s.charAt(i), nextNode);
node = node.next;
}
}
}