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
王晓博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:java stringbuilder清空的方法