Java 循环后清除字符串缓冲区/构建器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2242471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clearing a string buffer/builder after loop
提问by waterfallrain
How do you clear the string buffer in Java after a loop so the next iteration uses a clear string buffer?
如何在循环后清除 Java 中的字符串缓冲区,以便下一次迭代使用清除字符串缓冲区?
回答by Jon
One option is to use the delete method as follows:
一种选择是使用删除方法,如下所示:
StringBuffer sb = new StringBuffer();
for (int n = 0; n < 10; n++) {
sb.append("a");
// This will clear the buffer
sb.delete(0, sb.length());
}
Another option (bit cleaner) uses setLength(int len):
另一个选项(位清洁器)使用setLength(int len):
sb.setLength(0);
See Javadocfor more info:
有关更多信息,请参阅Javadoc:
回答by Suraj Chandran
buf.delete(0, buf.length());
回答by Eli Acherkan
I suggest creating a new StringBuffer
(or even better, StringBuilder
) for each iteration. The performance difference is really negligible, but your code will be shorter and simpler.
我建议为每次迭代创建一个新的StringBuffer
(甚至更好的StringBuilder
)。性能差异确实可以忽略不计,但您的代码会更短更简单。
回答by Mossaddeque Mahmood
The easiest way to reuse the StringBuffer
is to use the method setLength()
重用最简单的方法StringBuffer
是使用方法setLength()
public void setLength(int newLength)
public void setLength(int newLength)
You may have the case like
你可能有这样的情况
StringBuffer sb = new StringBuffer("HelloWorld");
// after many iterations and manipulations
sb.setLength(0);
// reuse sb
回答by Binh Phung
StringBuffer sb = new SringBuffer();
// do something wiht it
sb = new StringBuffer();
i think this code is faster.
我认为这段代码更快。
回答by Aditya Singh
You have two options:
您有两个选择:
Either use:
要么使用:
sb.setLength(0); // It will just discard the previous data, which will be garbage collected later.
Or use:
或使用:
sb.delete(0, sb.length()); // A bit slower as it is used to delete sub sequence.
NOTE
笔记
Avoid declaring StringBuffer
or StringBuilder
objects within the loop else it will create new objects with each iteration. Creating of objects requires system resources, space and also takes time. So for long run, avoid declaring them within a loop if possible.
避免在循环中声明StringBuffer
或StringBuilder
对象,否则每次迭代都会创建新对象。创建对象需要系统资源、空间并且也需要时间。所以从长远来看,尽可能避免在循环中声明它们。
回答by Hao
Already good answer there. Just add a benchmark result for StringBuffer and StringBuild performance difference use new instance in loop or use setLength(0) in loop.
那里已经很好的答案了。只需为 StringBuffer 和 StringBuild 性能差异添加一个基准结果,在循环中使用新实例或在循环中使用 setLength(0)。
The summary is: In a large loop
总结是:在一个大循环中
- StringBuilder is much faster than StringBuffer
- Create new StringBuilder instance in loop have no difference with setLength(0). (setLength(0) have very very very tiny advantage than create new instance.)
- StringBuffer is slower than StringBuilder by create new instance in loop
- setLength(0) of StringBuffer is extremely slower than create new instance in loop.
- StringBuilder 比 StringBuffer 快得多
- 在循环中创建新的 StringBuilder 实例与 setLength(0) 没有区别。(setLength(0) 比创建新实例有非常非常非常微小的优势。)
- 通过在循环中创建新实例,StringBuffer 比 StringBuilder 慢
- StringBuffer 的 setLength(0) 比在循环中创建新实例慢得多。
Very simple benchmark (I just manually changed the code and do different test ):
非常简单的基准测试(我只是手动更改了代码并进行了不同的测试):
public class StringBuilderSpeed {
public static final char ch[] = new char[]{'a','b','c','d','e','f','g','h','i'};
public static void main(String a[]){
int loopTime = 99999999;
long startTime = System.currentTimeMillis();
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < loopTime; i++){
for(char c : ch){
sb.append(c);
}
sb.setLength(0);
}
long endTime = System.currentTimeMillis();
System.out.println("Time cost: " + (endTime - startTime));
}
}
}
New StringBuilder instance in loop: Time cost: 3693, 3862, 3624, 3742
循环中的新 StringBuilder 实例:时间成本:3693、3862、3624、3742
StringBuilder setLength: Time cost: 3465, 3421, 3557, 3408
StringBuilder setLength:时间成本:3465、3421、3557、3408
New StringBuffer instance in loop: Time cost: 8327, 8324, 8284
循环中的新 StringBuffer 实例:时间成本:8327、8324、8284
StringBuffer setLength Time cost: 22878, 23017, 22894
StringBuffer setLength 时间成本:22878、23017、22894
Again StringBuilder setLength to ensure not my labtop got some issue to use such long for StringBuffer setLength :-) Time cost: 3448
再次 StringBuilder setLength 以确保我的实验室不会遇到一些问题来使用如此长的 StringBuffer setLength :-) 时间成本:3448
回答by Ido Kessler
public void clear(StringBuilder s) {
s.setLength(0);
}
Usage:
用法:
StringBuilder v = new StringBuilder();
clear(v);
for readability, I think this is the best solution.
为了可读性,我认为这是最好的解决方案。