StringBuffer

来源:互联网 发布:c语言百分号什么意思 编辑:程序博客网 时间:2024/06/07 12:53

StringBuffer是可变长的字符串



  • 追加 删除 插入 反转
append追加 
delete 删除 
insert 插入 
reverse 反转
public class TestString {      public static void main(String[] args) {        String str1 = "let there ";         StringBuffer sb = new StringBuffer(str1); //根据str1创建一个StringBuffer对象        sb.append("be light"); //在最后追加                 System.out.println(sb);                 sb.delete(4, 10);//删除4-10之间的字符                 System.out.println(sb);                 sb.insert(4, "there ");//在4这个位置插入 there                 System.out.println(sb);                 sb.reverse(); //反转                 System.out.println(sb);     }  }

  • 长度 容量
为什么StringBuffer可以变长? 
和String内部是一个字符数组一样,StringBuffer也维护了一个字符数组。 但是,这个字符数组,留有冗余长度 
比如说new StringBuffer("the"),其内部的字符数组的长度,是19,而不是3,这样调用插入和追加,在现成的数组的基础上就可以完成了。 
如果追加的长度超过了19,就会分配一个新的数组,长度比原来多一些,把原来的数据复制到新的数组中,看上去 数组长度就变长了

public class TestString {      public static void main(String[] args) {        String str1 = "the";         StringBuffer sb = new StringBuffer(str1);                 System.out.println(sb.length()); //内容长度                 System.out.println(sb.capacity());//总空间      }  }

  • StringBuffer性能
String与StringBuffer的性能区别?

生成10位长度的随机字符串
然后,先使用String的+,连接10000个随机字符串,计算消耗的时间
然后,再使用StringBuffer连接10000个随机字符串,计算消耗的时间

使用System.currentTimeMillis() 获取当前时间(毫秒)

public class TestString {     public static void main(String[] args) {        int total = 10000;        String s = randomString(10);        StringBuffer sb = new StringBuffer();                 String str1 = "";        long start = System.currentTimeMillis();                 for (int i = 0; i <total; i++) {            str1+=s;        }        long end = System.currentTimeMillis();        System.out.printf("使用字符串连接+的方式,连接%d次,耗时%d毫秒%n",total,end-start);        total *=100;        start = System.currentTimeMillis();        for (int i = 0; i <total; i++) {            sb.append(s);        }        end = System.currentTimeMillis();        System.out.printf("使用StringBuffer的方式,连接%d次,耗时%d毫秒%n",total,end-start);             }     private static String randomString(int length) {        String pool = "";        for (short i = '0'; i <= '9'; i++) {            pool += (char) i;        }        for (short i = 'a'; i <= 'z'; i++) {            pool += (char) i;        }        for (short i = 'A'; i <= 'Z'; i++) {            pool += (char) i;        }        char cs[] = new char[length];        for (int i = 0; i < cs.length; i++) {            int index = (int) (Math.random() * pool.length());            cs[i] = pool.charAt(index);        }        String result = new String(cs);        return result;    } }


原创粉丝点击