java如何删除字符串中的重复项,例如:“我的名字是这个和那个这个和那个”输出将是“我的名字是这个和那个”
我试过这个
公共静态无效重复删除程序(){ //从用户中删除字符串中的所有重复单词
//ask the user to enter his/her input
System.out.println("Enter your sentence: ");
//get user input
Scanner string = new Scanner(System.in);
String UserInput = string.nextLine();
//add comas into string and create an array
String[] arrayString = UserInput.split(",");
System.out.print(Arrays.toString(arrayString));
//create a new array that stores the non duplictes
String[] newArray = new String[arrayString.length];
Arrays.asList(newArray);
//loop through array to find duplicates
for (int i = 0 ; i < arrayString.length; i ++){
for (int j = 0; i < arrayString.length; i ++){
//try to remove duplicates
if (i != j){
//这是我正在努力解决的问题,在循环通过数组后,我如何删除重复项
String a = Integer.toString(i);
String b = Integer.toString(j);
newArray.add(a);
}
# 1 楼答案
这似乎是更简单的方法:
使用Java8流重写上述内容
# 2 楼答案
在阵列中,它将工作
}