在Java中将整数数组转换为字符串的最佳方法 [英] Best way to convert an array of integers to a string in Java
本文介绍了在Java中将整数数组转换为字符串的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java中,我有一个整数数组.有没有一种快速的方法可以将它们转换为字符串?
In Java, I have an array of integers. Is there a quick way to convert them to a string?
I.E. int[] x = new int[] {3,4,5}
x toString()应该产生"345"
I.E. int[] x = new int[] {3,4,5}
x toString() should yield "345"
推荐答案
最简单的方法可能是StringBuilder:
Simplest performant approach is probably StringBuilder:
StringBuilder builder = new StringBuilder();
for (int i : array) {
builder.append(i);
}
String text = builder.toString();
If you find yourself doing this in multiple places, you might want to look at Guava's Joiner
class - although I don't believe you'll be able to use it for primitive arrays. As pointed out below, you can use Ints.join
for this.
这篇关于在Java中将整数数组转换为字符串的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文