需要弄清楚如何替换 space 的数组列表中的逗号
Need to figure out how to replace the comma in the array list for a space
我已经尝试使用 replaceAll
和 removeAll
但仍然无法更改没有逗号的列表。
我试着在 list + "") 之后添加;但没有任何改变。仍然得到带逗号的输出。
我需要删除输出中的逗号。数字之间只需要一个白色 space。 (见最后的输出)
(Remove duplicates) Write a method that removes the duplicate elements from an array list of integers using the following header:
public static void removeDuplicate(ArrayList<Integer> list)
Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space.
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);
}
}
输出:我需要删除输出中的逗号。数字之间只需要一个白色 space。
运行:
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
实现,而是将其转换为您自己需要的字符串。在这里,您可以流式传输列表并使用 space:
加入它
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(" "))
);
由于问题是用 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
在这个程序中有两种方法可以替换白色space的逗号。
长篇:
需要添加:
导入 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
和 removeAll
但仍然无法更改没有逗号的列表。
我试着在 list + "") 之后添加;但没有任何改变。仍然得到带逗号的输出。
我需要删除输出中的逗号。数字之间只需要一个白色 space。 (见最后的输出)
(Remove duplicates) Write a method that removes the duplicate elements from an array list of integers using the following header:
public static void removeDuplicate(ArrayList<Integer> list)
Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space.
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);
}
}
输出:我需要删除输出中的逗号。数字之间只需要一个白色 space。 运行:
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
实现,而是将其转换为您自己需要的字符串。在这里,您可以流式传输列表并使用 space:
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(" "))
);
由于问题是用 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
在这个程序中有两种方法可以替换白色space的逗号。
长篇: 需要添加: 导入 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);
}
}