需要弄清楚如何将数组列表中的逗号替换为空格



我已经尝试使用replaceAllremoveAll,但仍然无法改变没有逗号的列表。我试着在列表后加上+ ");但一切都没有改变。仍然得到带逗号的输出。我需要删除输出中的逗号。数字之间只需要一个空格。(见输出末尾)


public static void removeDuplicate(ArrayList<Integer> list)
编写一个测试程序,提示用户输入10个整数到一个整数列表中,并显示由一个空格分隔的不同整数。
package exercise11_13;
import java.util.ArrayList;
import java.util.Scanner;
public class Exercise11_13 {
/**
* @param args the command line arguments
*/
// Main method
public static void main(String[] args) {
// Create de Scanner object.
Scanner input = new Scanner(System.in);

// Prompt the user to enter ten integer numbers. 
System.out.print("Please, enter ten (10) integers numbers: ");

// Create the ListArray (List object).
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 10; i++) list.add(input.nextInt());
removeDuplicate(list);

System.out.println("Display the distinct integers in their input order "
+ "and seperated by exactly one space: " + "n" + list );
}
public static void removeDuplicate(ArrayList<Integer> list) {
ArrayList<Integer> temp = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (!temp.contains(list.get(i))) {
temp.add(list.get(i));
}
}
list.clear();
list.addAll(temp);

}
}

输出:我需要删除输出中的逗号。数字之间只需要一个空格。运行:

Please, enter ten (10) integers numbers: 78
99
54
63
56
78
63
14
67
99
Display the distinct integers in their input order and seperated by exactly one space: 
[78, 99, 54, 63, 56, 14, 67]
BUILD SUCCESSFUL (total time: 27 seconds)

当您使用这样的对象时,它会通过其toString方法隐式地转换为字符串。您不应该依赖于列表的toString实现,而是将其转换为您自己需要的字符串。在这里,您可以流式传输列表并使用空格将其连接起来:

System.out.println(
"Display the distinct integers in their input order and separated by exactly one space: n" + 
list.stream().map(Object::toString).collect(Collectors.joining(" "))
);

在本程序中,有两种方法可以替换空格中的逗号:

长:需要补充:进口java.util.stream.Collectors;

System.out.println("Display the distinct integers in their input "
+ "order and separated by exactly one space: "
+ "n" 
+ 
list.stream().map(Object::toString).collect(Collectors.joining("")));

短的:

System.out.println("nDisplay the distinct integers in their input "
+ "order and separated by exactly one space: "
+ "n" + list.toString().replaceAll("[,\[\]]", ""));  

在整个程序下面。

package exercise11_13;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.stream.Collectors;
public class Exercise11_13 {

/**
* @param args the command line arguments
*/
// Main method
public static void main(String[] args) {
// Create de Scanner object.
Scanner input = new Scanner(System.in);

// Prompt the user to enter ten integer numbers. 
System.out.print("Please, enter ten (10) integers numbers: ");

// Create the ListArray (List object).
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 10; i++) list.add(input.nextInt());

removeDuplicate(list);
/**
System.out.println("Display the distinct integers in their input "
+ "order and separated by exactly one space: "
+ "n" + 
list.stream().map(Object::toString).collect(Collectors.joining(" ")));
*/
System.out.println("nDisplay the distinct integers in their input "
+ "order and separated by exactly one space: "
+ "n" + list.toString().replaceAll("[,\[\]]", ""));         
}

public static void removeDuplicate(ArrayList<Integer> list) {

ArrayList<Integer> temp = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {

if (!temp.contains(list.get(i))) {
temp.add(list.get(i));
}
}
list.clear();
list.addAll(temp);

}
}

由于问题被标记为replaceAll,因此确实有可能删除所有"冗余的";List::toString方法(方括号和逗号)使用正则表达式[,[]](方括号应用转义)提供的字符:

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
System.out.println(list.toString().replaceAll("[,\[\]]", "")); // 1 2 3 4 5

或者,知道括号是第一个和最后一个字符,可以在substring的帮助下删除它们,然后应用String::replace:

String str = list.toString();

System.out.println(str.substring(1, str.length() - 1).replace(",", "")); // 1 2 3 4 5

最新更新