概述
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清空的方法
最后
以上就是完美黑米为你收集整理的android stringbuilder 清空,java stringbuilder清空的方法的全部内容,希望文章能够帮你解决android stringbuilder 清空,java stringbuilder清空的方法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复