我有一个 List
包含370个元素,每个元素都是List <String>
包含 4 个字符串。我正在通过这样做来制作所有数据的一大串:
String veryBigString = "";
for (List <String> s : myList) {
for (String e : s){
veryBigString += e;
}
}
但是,这需要很长时间。是否有任何选项可以加快速度?
编辑:像你们许多人建议的那样使用 StringBuilder,我得到 java.lang.OutOfMemoryError
.也许我做错了什么:
StringBuilder sb = new StringBuilder();
for (List <String> s : birdsInfoList) {
for (String e : s){
sb.append(e);
}
}
Log.i("Very big String", String.valueOf(sb));
您应该使用 StringBuilder,然后使用连接。更好的性能明智。
StringBuilder sb = new StringBuilder("Hello")
.append("World");
有关这方面的更多信息,您可以阅读 this .
来自 Oracle 文档:
StringBuilder objects are like String objects, except that they can be
modified. Internally, these objects are treated like variable-length
arrays that contain a sequence of characters. At any point, the length
and content of the sequence can be changed through method invocations.
Strings should always be used unless string builders offer an
advantage in terms of simpler code (see the sample program at the end
of this section) or better performance. For example, if you need to
concatenate a large number of strings, appending to a StringBuilder
object is more efficient.
阅读此处:http://docs.oracle.com/javase/tutorial/java/data/buffers.html