用逗号拆分数组以分隔每个元素
Splitting an array with commas to separate each elemnt
我有一个打印出来的输出:1 2 3 4 5
我希望输出为:1,2,3,4,5
当我打印最终数组时,它看起来像:System.out.println(D);
我应该添加什么以满足我的需要。
欢迎大家回答。
将(typeof x)
替换为数组的元素类型(或者将此代码放在通用函数中以获得奖励积分,但它不适用于原始类型):
StringBuilder out = (new StringBuilder());
boolean first = true;
for ((typeof x) x : D) {
if (!first) {
out.append(",")
}
out.append(x.toString());
first = false;
}
return out.toString();
您可以创建自己的方法来打印结果,例如这个:
for (int i = 0; i < D.length; i++) {
System.out.print(D[i]);
if (i != D.length-1){
System.out.print(",");
}
}
我认为您正在尝试用逗号而不是空格打印出一个数组。
for (int i = 0; i < arr.length() - 1; i++) {
System.out.print(arr[i]);
System.out.print(',');
}
System.out.println(arr[arr.length() - 1]);
你可以单独打印数组的元素,像这样:
String output = "";
//go through all elements in D
for (int i =0;i<D.length;i++){
//add the Integer to the String
output = output+i;
//add , if not the last element
if (i<D.length-1){
output = output+",";
}
}
//Print it out
System.out.println(output);
试试这个代码:
如果您的数据类型是整数数组:
int my_array={1,2,3,4,5};
for(int i=0; i < my_array.length; i++) {
System.out.print(my_array[i] + ",");//this line will print the value with a coma (,)
}
如果你的数据类型是字符串;
String my_number="1 2 3 4 5";
for(int i=0; i < my_number.length; i++){
if(my_number.toCharArray()[i]!=' ')
System.out.print(my_number.toCharArray()[i]+",");
}
或者,
String my_number="1 2 3 4 5";
my_number = my_number.replace(' ', ',');//this method (replace) will replace all space(' ') by coma(',')
System.out.println(my_number);
为什么不简单地使用 Arrays.toString
public static String toString(int[] a) Returns a string representation
of the contents of the specified array. The string representation
consists of a list of the array's elements, enclosed in square
brackets ("[]"). Adjacent elements are separated by the characters ",
" (a comma followed by a space). Elements are converted to strings as
by String.valueOf(int). Returns "null" if a is null. Parameters: a -
the array whose string representation to return Returns: a string
representation of a Since:
1.5
尝试
System.out.println (Arrays.toString (D));
如果不需要空格,则可以 replaced
使用
System.out.println (Arrays.toString (D).replace(" ", ""));
我有一个打印出来的输出:1 2 3 4 5
我希望输出为:1,2,3,4,5
当我打印最终数组时,它看起来像:System.out.println(D);
我应该添加什么以满足我的需要。
欢迎大家回答。
将(typeof x)
替换为数组的元素类型(或者将此代码放在通用函数中以获得奖励积分,但它不适用于原始类型):
StringBuilder out = (new StringBuilder());
boolean first = true;
for ((typeof x) x : D) {
if (!first) {
out.append(",")
}
out.append(x.toString());
first = false;
}
return out.toString();
您可以创建自己的方法来打印结果,例如这个:
for (int i = 0; i < D.length; i++) {
System.out.print(D[i]);
if (i != D.length-1){
System.out.print(",");
}
}
我认为您正在尝试用逗号而不是空格打印出一个数组。
for (int i = 0; i < arr.length() - 1; i++) {
System.out.print(arr[i]);
System.out.print(',');
}
System.out.println(arr[arr.length() - 1]);
你可以单独打印数组的元素,像这样:
String output = "";
//go through all elements in D
for (int i =0;i<D.length;i++){
//add the Integer to the String
output = output+i;
//add , if not the last element
if (i<D.length-1){
output = output+",";
}
}
//Print it out
System.out.println(output);
试试这个代码:
如果您的数据类型是整数数组:
int my_array={1,2,3,4,5};
for(int i=0; i < my_array.length; i++) {
System.out.print(my_array[i] + ",");//this line will print the value with a coma (,)
}
如果你的数据类型是字符串;
String my_number="1 2 3 4 5";
for(int i=0; i < my_number.length; i++){
if(my_number.toCharArray()[i]!=' ')
System.out.print(my_number.toCharArray()[i]+",");
}
或者,
String my_number="1 2 3 4 5";
my_number = my_number.replace(' ', ',');//this method (replace) will replace all space(' ') by coma(',')
System.out.println(my_number);
为什么不简单地使用 Arrays.toString
public static String toString(int[] a) Returns a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space). Elements are converted to strings as by String.valueOf(int). Returns "null" if a is null. Parameters: a - the array whose string representation to return Returns: a string representation of a Since: 1.5
尝试
System.out.println (Arrays.toString (D));
如果不需要空格,则可以 replaced
使用
System.out.println (Arrays.toString (D).replace(" ", ""));