Java 中将数组转换为字符串
作者:互联网
在示例中,我们创建了一个 int
类型的数组 arrayOfInts
,并用一些项目填充它。要将 arrayOfInts
转换为字符串,我们使用 Arrays.toString()
并将其作为参数传递,该参数返回我们在输出中打印的字符串 arrayToString
。
---------------------------------------------
import java.util.Arrays;
public class ArrayToString {
public static void main(String[] args) {
int[] arrayOfInts = {1, 3, 9, 11, 13};
String arrayToString = Arrays.toString(arrayOfInts);
System.out.println(arrayToString);
}
}
输出:
[1, 3, 9, 11, 13]
---------------------------------------------
public class ArrayToString {
public static void main(String[] args) {
String[] arrayOfStrings = {"One", "Two", "Three", "four", "Five"};
String arrayToString = String.join(" ", arrayOfStrings);
System.out.println(arrayToString);
}
}
输出:
One Two Three four Five
---------------------------------------------
import java.util.Arrays;
import java.util.stream.Collectors;
public class ArrayToString {
public static void main(String[] args) {
String[] arrayOfStrings = {"One", "Two", "Three", "four", "Five"};
String arrayToString = Arrays.stream(arrayOfStrings).collect(Collectors.joining());
System.out.println(arrayToString);
}
}
输出:
OneTwoThreefourFive
---------------------------------------------
标签:Java,String,Arrays,arrayToString,---------------------------------------------,数 来源: https://www.cnblogs.com/xiadongqing/p/15614268.html