转换的字符串数组在Java中的字符串 [英] Convert array of strings into a string in Java

查看:138
本文介绍了转换的字符串数组在Java中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java code转换字符串数组为一个字符串。

I want the Java code for converting an array of strings into an string.

推荐答案

或者使用

Arrays.toString()

StringBuilder builder = new StringBuilder();
for(String s : arr) {
    builder.append(s);
}
return builder.toString();

您可以修改上述取决于什么角色,如果有的话,你想在字符串之间。

You can modify the above depending on what characters, if any, you want in between strings.

您还可以看到几乎相同的code以上,但使用的StringBuffer - 的StringBuilder 是一个较新类不是线程安全的,但因此具有更好的性能,在单个线程,因为它摒弃了不必要的同步。总之,你最好使用的StringBuilder 的情况下,99% - 功能明智的,两者是相同的。

You may also see near identical code to the above but using StringBuffer - StringBuilder is a newer class that's not thread-safe, but therefore has better performance in a single thread because it does away with unneeded synchronization. In short, you're better using StringBuilder in 99% of cases - functionality wise, the two are identical.

不要:使用一个字符串,然后追加到它与+ =像一些答案在这里展示。这通过屋顶,因为你创建和扔掉许多字符串对象,你有你的数组中的项目发送GC。对于小数组,你可能没有真正注意到其中的差别,但路数也可以是数量级的速度较慢的订单。

DON'T use a string and just append to it with += like some of the answers show here. This sends the GC through the roof because you're creating and throwing away as many string objects as you have items in your array. For small arrays you might not really notice the difference, but for large ones it can be orders of magnitude slower.

这篇关于转换的字符串数组在Java中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆