S
Shawn
Hi,
I am using a StringBuffer to hold a line with fixed length of 72 chars.
Later, I am going to put char at a specified location, using
setCharAt(location, char) method.
But this code doesn't work:
<Java>
StringBuffer line = new StringBuffer(72);
line.setCharAt(5, 'A');
</Java>
This code works:
<Java>
StringBuffer line = new StringBuffer(72);
for (int i=0; i<72; i++) //make the StringBuffer contains 72 empty chars
{
line.append(' ');
}
line.setCharAt(5, 'A');
</Java>
I found that this required for loop is ridiculous. Reading the Java
document of StringBuffer suggests the first version is correct and
should work. I don't understand it.
Thank you very much for your help.
I am using a StringBuffer to hold a line with fixed length of 72 chars.
Later, I am going to put char at a specified location, using
setCharAt(location, char) method.
But this code doesn't work:
<Java>
StringBuffer line = new StringBuffer(72);
line.setCharAt(5, 'A');
</Java>
This code works:
<Java>
StringBuffer line = new StringBuffer(72);
for (int i=0; i<72; i++) //make the StringBuffer contains 72 empty chars
{
line.append(' ');
}
line.setCharAt(5, 'A');
</Java>
I found that this required for loop is ridiculous. Reading the Java
document of StringBuffer suggests the first version is correct and
should work. I don't understand it.
Thank you very much for your help.