java stringbuilder清空的本领

java stringbuilder清空的本领

java stringbuilder清空的三种本领

1、新天生一个,旧的由体例机动接收

2、delete 本领

builder1.delete(0, builder.length());

3、Length

StringBuilder val = new StringBuilder();

val.Append(“….”);

val.Length = 0;//清空

StringBuilder builder = new StringBuilder();

long time = System.currentTimeMillis();

for(int i=0;i<10000000;i++){

builder = new StringBuilder();

builder.append("aa");

builder.append("bb");

builder.append("cc");

builder.append("dd");

builder.append("ee");

}

System.out.println("new 耗费时间:" + (System.currentTimeMillis() - time));

long time1 = System.currentTimeMillis();

StringBuilder builder1 = new StringBuilder();

for(int i=0;i<10000000;i++){

builder1.delete(0, builder.length());

builder1.append("a");

builder1.append("b");

builder1.append("c");

builder1.append("d");

builder1.append("e");

}

System.out.println("delete 耗费时间:" + (System.currentTimeMillis() - time1));

long time2 = System.currentTimeMillis();

StringBuilder builder2 = new StringBuilder();

for(int i=0;i<10000000;i++){

builder2.setLength(0);

builder2.append("1a");

builder2.append("1b");

builder2.append("1c");

builder2.append("1d");

builder2.append("1e");

}

System.out.println("setLenth=0 耗费时间:" + (System.currentTimeMillis() - time2));

截止一:

new 耗费时间:485

delete 耗费时间:471

setLenth=0 耗费时间:385

截止二:

new 耗费时间:621

delete 耗费时间:411

setLenth=0 耗费时间:423

分享到 :
相关推荐

Leave a Reply

Your email address will not be published. Required fields are marked *