在 Java 中将整数数组转换为字符串的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3728563/
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
Best way to convert an array of integers to a string in Java
提问by didxga
In Java, I have an array of integers. Is there a quick way to convert them to a string?
在 Java 中,我有一个整数数组。有没有一种快速的方法将它们转换为字符串?
I.E. int[] x = new int[] {3,4,5}
x toString() should yield "345"
IE int[] x = new int[] {3,4,5}
x toString() 应该产生“345”
回答by Jon Skeet
Simplest performant approach is probably StringBuilder:
最简单的高性能方法可能是 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. EDIT: As pointed out below, you can use Ints.join
for this.
如果你发现自己在多个地方这样做,你可能想看看Guava 的Joiner
类- 尽管我不相信你能够将它用于原始数组。编辑:正如下面所指出的,你可以使用Ints.join
它。
回答by Landei
int[] x = new int[] {3,4,5};
String s = java.util.Arrays.toString(x).replaceAll("[\,\[\]\ ]", "")
Update
更新
For completeness the Java 8 Streams solution, but it isn't pretty (libraries like vavrwould be shorter and faster):
为了完整性,Java 8 Streams 解决方案,但它并不漂亮(像vavr这样的库会更短更快):
String s = IntStream.of(x)
.mapToObj(Integer::toString)
.collect(Collectors.joining(""));
回答by Kekis2014
public static void main(String[] args) {
int[] dice = {1, 2, 3, 4, 5, 0};
String string = "";
for (int i = 0; i < dice.length; i++) {
string = string + dice[i];
}
System.out.print(string);
}
This is another way you can do it. Basically just makes a for-loop that accesses every element in the integer array and adds it to the string.
这是您可以做到的另一种方式。基本上只是创建一个 for 循环来访问整数数组中的每个元素并将其添加到字符串中。
回答by Sachin Shanbhag
Try with this - you have to import java.util.Arrays
and then -
试试这个 - 你必须import java.util.Arrays
然后 -
String temp = Arrays.toString( intArray ).replace(", ", "");
String finalStr = temp.substring(1, temp.length()-2);
Where intArray is your integer array.
其中 intArray 是您的整数数组。
回答by Jigar Joshi
StringBuffer str =new StringBuffer();
for(int i:x){
str.append(i);
}
You need to read all once at least.
您至少需要阅读一遍。
回答by Code
public static void main(String[] args) {
System.out.println("Integer Value :" +convertIntToString(new int[]{3,4,5}));
}
public static String convertIntToString(int intArray[]) {
List<Integer> listInteger = new ArrayList<Integer>();
for (int i = 0; i < intArray.length; i++) {
listInteger.add(intArray[i]);
}
Object o = listInteger;
return String.valueOf(o).replace("[", "").trim().replaceAll(", ","").trim().replaceAll("\]","").trim();
}
回答by ddd
public static void main(String[] args) {
System.out.println("Integer Value :" +convertIntToString(new int[]{3,4,5}));
}
public static String convertIntToString(int intArray[]) {
List<Integer> listInteger = new ArrayList<Integer>();
for (int i = 0; i < intArray.length; i++) {
listInteger.add(intArray[i]);
}
Object o = listInteger;
return String.valueOf(o).replace("[", "").trim().replaceAll(", ","").trim().replaceAll("\]","").trim();
}