Java 将链接列表附加到另一个链接列表
我是计算机科学专业的一年级学生,我想知道你能否帮我清理MyStringBuilder类中的append(StringBuilder b)方法。我把这个问题追溯到我最后一次尝试在我的测试驱动程序的链表之间来回追加。我在每一步之前都插入了调试打印语句,它似乎工作得很好。。。每个字符串生成器对象仍应彼此独立存在。谢谢Java 将链接列表附加到另一个链接列表,java,linked-list,Java,Linked List,我是计算机科学专业的一年级学生,我想知道你能否帮我清理MyStringBuilder类中的append(StringBuilder b)方法。我把这个问题追溯到我最后一次尝试在我的测试驱动程序的链表之间来回追加。我在每一步之前都插入了调试打印语句,它似乎工作得很好。。。每个字符串生成器对象仍应彼此独立存在。谢谢 //SAMPLE OUTPUT (what its supposed to be) **Testing constructor methods this is a string
//SAMPLE OUTPUT (what its supposed to be)
**Testing constructor methods
this is a string
another string
Testing Append methods
this is a string another string
this is a string another string and another
this is a string another string and another another string
this is a string another string and another another string!!
another string different strings?
...appending data**
//MY CURRENT OUTPUT
**Testing constructor methods
this is a string
another string
Testing Append methods
this is a string another string
this is a string another string and another
this is a string another string and another another string
this is a string another string different strings?
another string different strings?
...appending data**
到目前为止,我的字符串生成器类
public class MyStringBuilder
{
private CNode firstC; //first character node
private CNode lastC; //last character node
private int length; //length of mystringbuilder object
public MyStringBuilder(String s) //this one should is correct since it was given!
{
if (s == null || s.length() == 0) // Special case for empty String
{ // or null reference
firstC = null;
lastC = null;
length = 0;
}
else
{
// Create front node to get started
firstC = new CNode(s.charAt(0));
length = 1;
CNode currNode = firstC;
// Create remaining nodes, copying from String. Note
// how each new node is simply added to the end of the
// previous one. Trace this to see what is going on.
for (int i = 1; i < s.length(); i++)
{
CNode newNode = new CNode(s.charAt(i));
currNode.next = newNode;
currNode = newNode;
length++;
}
lastC = currNode;
}
}
// Create a new MyStringBuilder initialized with the chars in array s
public MyStringBuilder(char [] s)
{
if (s == null || s.length == 0) // Special case for empty char array
{ // or null reference
firstC = null;
lastC = null;
length = 0;
}
else
{
// Create front node to get started
firstC = new CNode(s[0]);
length = 1;
CNode currNode = firstC;
// Create remaining nodes, copying from char array.
for (int i = 1; i < s.length; i++)
{
CNode newNode = new CNode(s[i]);
currNode.next = newNode;
currNode = newNode;
length++;
}
lastC = currNode;
}
}
// Create a new empty MyStringBuilder
public MyStringBuilder()
{
firstC=null;
lastC=null;
length=0;
}
// Append MyStringBuilder b to the end of the current MyStringBuilder, and
// return the current MyStringBuilder. Be careful for special cases!
public MyStringBuilder append(MyStringBuilder b)
{
if(length==0)
{
firstC = new CNode(b.firstC.data);
length = 1;
CNode currNode = b.firstC;
for (int i = 1; i < b.length; i++)
{
CNode newNode = new CNode(currNode.next.data);
currNode.next = newNode;
currNode = newNode;
length++;
}
lastC = currNode;
}
else{//works
CNode currNode = lastC;
CNode newNode = b.firstC;
for (int i = 1; i < b.length+1; i++)
{
currNode.next = newNode;
currNode = newNode;
newNode = currNode.next;
length++;
}
lastC = currNode;
}
return b;
}
// Append String s to the end of the current MyStringBuilder, and return
// the current MyStringBuilder. Be careful for special cases!
public MyStringBuilder append(String s)
{
if (s == null ) // Special case for null ref
{
throw new NullPointerException("attempting to add empty string");
}
else // non null ref
{
//convert string argument to temp char array to be passed into nodes
char [] tempCArray = s.toCharArray();
if (isEmpty()) // if stringbuilder is empty
{
// Create front node to get started
firstC = new CNode(tempCArray[0]);
length = 1;
CNode currNode = firstC;
// Create remaining nodes, copying from temp char array.
for (int i = 1; i < tempCArray.length; i++)
{
CNode newNode = new CNode(tempCArray[i]);
currNode.next = newNode;
currNode = newNode;
length++;
lastC = currNode;
}
}
//is stringbuilder is not empty
else {
CNode currNode = lastC;
//if string builder is not empty
// Create nodes, copying from temp char array.
for (int i = 0; i < tempCArray.length; i++) {
CNode newNode = new CNode(tempCArray[i]);
currNode.next = newNode;
currNode = newNode;
length++;
lastC = currNode;
}
}
}
return this;
}
// Append char array c to the end of the current MyStringBuilder, and
// return the current MyStringBuilder. Be careful for special cases!
public MyStringBuilder append(char [] c)
{
if (c == null || c.length == 0) // Special case for empty char array
{ // or null reference
throw new NullPointerException("attempting to add empty character array");
}
else
{
if (isEmpty()) // if stringbuilder is empty
{
// Create front node to get started
firstC = new CNode(c[0]);
length = 1;
CNode currNode = firstC;
// Create remaining nodes, copying from char array.
for (int i = 1; i < c.length; i++)
{
CNode newNode = new CNode(c[i]);
currNode.next = newNode;
currNode = newNode;
length++;
lastC = currNode;
}
}
//is stringbuilder is not empty
else {
CNode currNode = lastC;
//if string builder is not empty
// Create nodes, copying from char array.
for (int i = 0; i < c.length; i++) {
CNode newNode = new CNode(c[i]);
currNode.next = newNode;
currNode = newNode;
length++;
lastC = currNode;
}
}
}
return this;
}
// Append char c to the end of the current MyStringBuilder, and
// return the current MyStringBuilder. Be careful for special cases!
public MyStringBuilder append(char c)
{
CNode newNode = new CNode(c);
if (isEmpty())
{
firstC = newNode;
lastC = newNode;
} //if stringbuilder object is empty
else
{
CNode currNode = lastC;
currNode.next=newNode;
currNode = newNode;
lastC = currNode;
} // if stringbuilder object is not empty
length++;
return this;
}
public String toString() //!!must change to eliminate +!!
{
CNode currNode= firstC;
char [] temp = new char [length];
int counter=0;
if (length == 0)
return "";
while (currNode.next !=null)
{
temp[counter]=currNode.data;
currNode = currNode.next;
counter++;
}
temp[counter]=currNode.data;
String result = new String (temp);
return result;
}
/** additional private methods **/
private boolean isEmpty()
{
if (length<1)
return true;
return false;
} //end of isEmpty
private class CNode
{
private char data;
private CNode next;
public CNode(char c)
{
data = c;
next = null;
}
public CNode(char c, CNode n)
{
data = c;
next = n;
}
} // end of CNode inner class
} //end of MyStringBuilder class
问题出在你自己身上
public MyStringBuilder append(MyStringBuilder b)
类中的方法MyStringBuilder
else块是错误的
} else {// works
CNode currNode = lastC;
CNode newNode = b.firstC;
for (int i = 1; i < b.length + 1; i++) {
currNode.next = newNode;
currNode = newNode;
newNode = currNode.next;
length++;
}
lastC = currNode;
}
欢迎来到堆栈溢出!请参阅和。您知道如何使用调试器吗?这是一个简单的调试。代码看起来并不复杂,只需逐行调试代码并观察观察观察窗口中的值即可。非常感谢@KrasimirStoev!这几乎可以正常工作,但现在在打印toString方法时,组合字符串的每段之间有16个空格。”代码“这是一个字符串这是一个字符串(16个空格)另一个这是一个字符串(16个空格)另一个字符串这是一个字符串(16个空格)另一个字符串!!”代码'是的,我现在就编辑我的答案-你必须添加'bNode=bNode.next;'在for循环的主体中
} else {// works
CNode currNode = lastC;
CNode newNode = b.firstC;
for (int i = 1; i < b.length + 1; i++) {
currNode.next = newNode;
currNode = newNode;
newNode = currNode.next;
length++;
}
lastC = currNode;
}
public MyStringBuilder append(MyStringBuilder b) {
if (length == 0) {
firstC = new CNode(b.firstC.data);
length = 1;
CNode currNode = b.firstC;
for (int i = 1; i < b.length; i++) {
CNode newNode = new CNode(currNode.next.data);
currNode.next = newNode;
currNode = newNode;
length++;
}
lastC = currNode;
} else {// works
CNode currNode = lastC;
CNode bNode = b.firstC;
for (int i = 1; i < b.length + 1; i++) {
CNode newNode = new CNode(bNode.data);
currNode.next = newNode;
currNode = newNode;
newNode = currNode.next;
bNode = bNode.next;
length++;
}
lastC = currNode;
}
return b;
}