android 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

王晓博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权

转载请注明原文链接:java stringbuilder清空的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值