我是CS专业的一年级学生,我想知道您是否可以帮助我清理MyStringBuilder类中的append(StringBuilder b)方法。 我上次尝试在我的测试驱动程序中的链表之间来回附加问题。我在这些之前的每一步都插入了调试打印语句,它似乎工作正常......每个字符串生成器对象仍应彼此独立存在。谢谢!
//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
测试驱动程序 ASSIG2AltTest
public class Assig2AltTest
{
public static void main(String [] args)
{
System.out.println("Testing constructor methods");
MyStringBuilder b1 = new MyStringBuilder("this is a string");
char [] c = {' ','a','n','o','t','h','e','r',' ','s','t','r','i','n','g'};
MyStringBuilder b2 = new MyStringBuilder(c);
MyStringBuilder b3 = new MyStringBuilder();
System.out.println(b1);
System.out.println(b2);
System.out.println(b3); // will show as an empty line
System.out.println("nTesting Append methods");
b1.append(b2);
System.out.println(b1);
b1.append(" and another");
System.out.println(b1);
b1.append(c);
System.out.println(b1);
b1.append('!'); b1.append('!'); // char append
b2.append(" different strings?");
System.out.println(b1); // Testing for independence of the StringBuilders
System.out.println(b2); // after append. b1 should be unchanged here
// Special case appending to empty object
b3.append("...appending data");
System.out.println(b3);
}
}
问题出在你的
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;
}
此代码执行以下操作 - this 中的节点现在指向 b
中的节点。这意味着当您在测试Assig2AltTest
中追加到b2
时,您实际上也会修改b1
,因为b1
已经开始指向来自b2
的节点。
您应该像这样编辑append(MyStringBuilder b)
方法:
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;
}