Java中StringBuffer类的setLength()方法

这两天在自学java,发现Java中StringBuffer类的setLength()方法有点奇怪,在此请教高手了!!!下面是一些源代码及运行结果:
代码1:
public class hello{
public static void main(String args[]){
StringBuffer str=new StringBuffer("hello world!");
System.out.println(str+" "+str.length()+" "+str.capacity());
str.setLength(10);//10小于str.length()=12
System.out.println(str+" "+str.length()+" "+str.capacity());
}
}
运行结果:
hello world! 12 28
hello worl 10 28
(ps:这个结果显而易见)
代码2:(ps:就是把str.setLength(10)中的10改为比str的length=12大的数13)
public class hello{
public static void main(String args[]){
StringBuffer str=new StringBuffer("hello world!");
System.out.println(str+" "+str.length()+" "+str.capacity());
str.setLength(13);//13大于str.length()=12
System.out.println(str+" "+str.length()+" "+str.capacity());
}
}
运行结果:
hello world! 12 28
hello world!
(ps:就是hello world!后面的输出内容都不能显示了,而且更奇怪的是,如果在上面代码中的最好一句后面添加输出语句,那么结果都显示不了)
代码3:(ps:就是在输出时,把str放到后面输出)
public class hello{
public static void main(String args[]){
StringBuffer str=new StringBuffer("hello world!");
System.out.println(str+" "+str.length()+" "+str.capacity());
str.setLength(13);//13大于str.length()=12
System.out.println(str.length()+" "+str.capacity()+" "+str);
}
}
运行结果:
hello world! 12 28
13 28 hello world!
(这结果又很正常)
是不是很奇怪?请高手们分析解决吧!!!

java.lang.StringBuffer.setLength() æ–¹æ³•è®¾ç½®å­—符序列的长度。该序列被改变到一个新的字符序列的参数所指定的长度。

#java.lang.StringBuffer.setLength()方法的声明
#public void setLength(int newLength)
#参数newLength -- è¿™æ˜¯æ–°çš„长度
#返回值:此方法不返回任何值
#异常:IndexOutOfBoundsException -- å¦‚æžœnewLength参数是负
#实例:使用java.lang.StringBuffer.setLength()方法
package com.yiibai;
import java.lang.*;
public class StringBufferDemo {
public static void main(String[] args) {
    StringBuffer buff = new StringBuffer("tutorials");
    System.out.println("buffer1 = " + buff);  
    // length of stringbuffer
    System.out.println("length = " + buff.length());
        
    // set the length of stringbuffer to 5
    buff.setLength(5);
        
    // print new stringbuffer value after changing length
    System.out.println("buffer2 = " + buff);
    // length of stringbuffer after changing length
    System.out.println("length = " + buff.length());
  }
}

#编译和运行上面的程序,这将产生以下结果:
buffer1 = tutorials
length = 9
buffer2 = tutor
length = 5
温馨提示:答案为网友推荐,仅供参考
第1个回答  2008-10-03
代码2没有问题,正常输出结果是:
hello world! 12 28
hello world! 13 28 //!到13间是:'\u0000'+空格
可能你具体哪里出现了问题。
!到13之间除了有一个空格外还会有一个'\u0000'字符,因为你增长了str的长度,java会在最后插入一个'\u0000'(即null空字符),这个字符不能正常复制,强行复制输出行这个字符包括后面的13 28都会被截掉(你的问题可能出在这里),但其实结果输出是正常的。
相应的,其实你的代码3的输出结果是
13 28 hello world!'\u0000' //最后面存在一个'\u0000'
第2个回答  2008-10-03
setLength
public void setLength(int newLength)设置字符序列的长度。序列将被更改为一个新的字符序列,新序列的长度由参数指定。对于每个小于 newLength 参数的非负索引 k,如果 k 小于原字符序列的长度,则新字符序列索引 k 处的字符与原字符序列索引 k 处的字符相同;否则,新字符序列索引 k 处的字符将是 null 字符 '\u0000'。换句话说,如果 newLength 参数小于当前长度,则长度将更改为指定的长度。
如果 newLength 参数大于或等于当前长度,则将追加有效的 null 字符 ('\u0000'),使长度满足 newLength 参数。

newLength 参数必须大于或等于 0。

参数:
newLength - 新长度
抛出:
IndexOutOfBoundsException - 如果 newLength 参数为负。
第3个回答  2008-10-03
你直接用cmd编译运行啊,没问题的。
第4个回答  2008-10-03
应该是你的编译器的问题!我想你是用JCreator吧!这个东西有点问题!要是你的JDK是1.6的话!你在cmd环境下编译是不会有这问题的!本回答被提问者采纳
相似回答