Java,从ArrayList中删除元素
我对这个项目有意见。基本前提是用户输入一个短语,它应该找到任何重复的单词以及有多少重复的单词 我的问题是只输入一个单词多次,例如。。。 你好你好你好 这方面的产出将是:Java,从ArrayList中删除元素,java,list,arraylist,iterator,Java,List,Arraylist,Iterator,我对这个项目有意见。基本前提是用户输入一个短语,它应该找到任何重复的单词以及有多少重复的单词 我的问题是只输入一个单词多次,例如。。。 你好你好你好 这方面的产出将是: "There are 2 duplicates of the word "hello" in the phrase you entered." "There are 1 duplicates of the word "hello" in the phrase you entered." 这似乎只有在这种情况下才会发生。如果
"There are 2 duplicates of the word "hello" in the phrase you entered."
"There are 1 duplicates of the word "hello" in the phrase you entered."
这似乎只有在这种情况下才会发生。如果我输入一个随机的短语,并将多个单词从输入到输出,它将显示正确的答案。我认为这个问题与删除重复的单词以及它在短语中重复了多少次有关,但我就是不能对它掉以轻心。我在任何地方都添加了打印行,并以各种方式改变了它的迭代次数,我在Java可视化工具中查看了它,但仍然找不到确切的问题。非常感谢您的帮助
这是我在线Java课程的作业,但它仅用于学习/实践,不适用于我的专业。我不是在寻找答案,只是寻求帮助。
public class DuplicateWords {
public static void main(String[] args) {
List<String> inputList = new ArrayList<String>();
List<String> finalList = new ArrayList<String>();
int duplicateCounter;
String duplicateStr = "";
Scanner scan = new Scanner(System.in);
System.out.println("Enter a sentence to determine duplicate words entered: ");
String inputValue = scan.nextLine();
inputValue = inputValue.toLowerCase();
inputList = Arrays.asList(inputValue.split("\\s+"));
finalList.addAll(inputList);
for(int i = 0; i < inputList.size(); i++) {
duplicateCounter = 0;
for(int j = i + 1; j < finalList.size(); j++) {
if(finalList.get(i).equalsIgnoreCase(finalList.get(j))
&& !finalList.get(i).equals("!") && !finalList.get(i).equals(".")
&& !finalList.get(i).equals(":") && !finalList.get(i).equals(";")
&& !finalList.get(i).equals(",") && !finalList.get(i).equals("\"")
&& !finalList.get(i).equals("?")) {
duplicateCounter++;
duplicateStr = finalList.get(i).toUpperCase();
}
if(finalList.get(i).equalsIgnoreCase(finalList.get(j))) {
finalList.remove(j);
}
}
if(duplicateCounter > 0) {
System.out.printf("There are %s duplicates of the word \"%s\" in the phrase you entered.", duplicateCounter, duplicateStr);
System.out.println();
}
}
}
}
公共类重复字{
公共静态void main(字符串[]args){
List inputList=新建ArrayList();
List finalList=new ArrayList();
整数重复计数器;
字符串duplicateStr=“”;
扫描仪扫描=新扫描仪(System.in);
System.out.println(“输入一个句子以确定输入的重复单词:”);
字符串inputValue=scan.nextLine();
inputValue=inputValue.toLowerCase();
inputList=Arrays.asList(inputValue.split(\\s+));
finalList.addAll(输入列表);
对于(int i=0;i0){
System.out.printf(“您输入的短语中有%s个单词\%s\”,duplicateCounter,duplicateStr);
System.out.println();
}
}
}
}
根据一些建议,我编辑了代码,但我不确定方向是否正确
String previous = "";
for(Iterator<String> i = inputList.iterator(); i.hasNext();) {
String current = i.next();
duplicateCounter = 0;
for(int j = + 1; j < finalList.size(); j++) {
if(current.equalsIgnoreCase(finalList.get(j))
&& !current.equals("!") && !current.equals(".")
&& !current.equals(":") && !current.equals(";")
&& !current.equals(",") && !current.equals("\"")
&& !current.equals("?")) {
duplicateCounter++;
duplicateStr = current.toUpperCase();
}
if(current.equals(previous)) {
i.remove();
}
}
if(duplicateCounter > 0) {
System.out.printf("There are %s duplicates of the word \"%s\" in the phrase you entered.", duplicateCounter, duplicateStr);
System.out.println();
}
}
String-previous=”“;
for(迭代器i=inputList.Iterator();i.hasNext();){
字符串电流=i.next();
重复计数器=0;
对于(int j=+1;j0){
System.out.printf(“您输入的短语中有%s个单词\%s\”,duplicateCounter,duplicateStr);
System.out.println();
}
}
在将输入列表
添加到最终列表
之前,从输入列表
中删除任何重复项。在将输入列表
添加到最终列表
之前,从输入列表
中删除任何重复项我将首先用每个单词填充映射
;每次遇到单词时,递增整数。差不多
String inputValue = scan.nextLine().toLowerCase();
String[] words = inputValue.split("\\s+");
Map<String, Integer> countMap = new HashMap<>();
for (String word : words) {
Integer current = countMap.get(word);
int v = (current == null) ? 1 : current + 1;
countMap.put(word, v);
}
我首先用每个单词填充一张地图
;每次遇到单词时,递增整数。差不多
String inputValue = scan.nextLine().toLowerCase();
String[] words = inputValue.split("\\s+");
Map<String, Integer> countMap = new HashMap<>();
for (String word : words) {
Integer current = countMap.get(word);
int v = (current == null) ? 1 : current + 1;
countMap.put(word, v);
}
您的代码的问题是,当您删除一个项目时,您仍然会增加索引,因此您会跳过下一个项目。在缩写形式中,您的代码是:
for (int j = i + 1; j < finalList.size(); j++) {
String next = finalList.get(i);
if (some test on next)
finalList.remove(next);
}
这将解决您的问题,但是,有一种更干净的方法:
String previous = "";
for (Iterator<String> i = inputList.iterator(); i.hasNext();) {
String current = i.next();
if (current.equals(previous)) {
i.remove(); // removes current item
}
previous = current;
}
如果您喜欢疼痛,请“手动”进行:
Set duplicates=new HashSet();//集合是唯一的
for(迭代器i=inputList.Iterator();i.hasNext();)
if(!duplicates.add(i.next())//add在集合更改时返回true
i、 删除();//删除当前项目
您的代码的问题是,当您删除一个项目时,您仍然会增加索引,因此您会跳过下一个项目。在缩写形式中,您的代码是:
for (int j = i + 1; j < finalList.size(); j++) {
String next = finalList.get(i);
if (some test on next)
finalList.remove(next);
}
这将解决您的问题,但是,有一种更干净的方法:
String previous = "";
for (Iterator<String> i = inputList.iterator(); i.hasNext();) {
String current = i.next();
if (current.equals(previous)) {
i.remove(); // removes current item
}
previous = current;
}
如果您喜欢疼痛,请“手动”进行:
Set duplicates=new HashSet();//集合是唯一的
for(迭代器i=inputList.Iterator();i.hasNext();)
if(!duplicates.add(i.next())//add在集合更改时返回true
i、 删除();//删除当前项目
您正在迭代ArrayList,同时从中删除项。这会导致意外行为。一种安全的方法是使用Iterator.remove()
,如果您想在删除数组项的同时迭代数组,我建议您从数组的末尾开始迭代到顶部。您在从数组中删除项的同时迭代数组列表。这会导致意外行为。一种安全的方法是使用Iterator.remove()
,请查看是否要在删除数组项的同时迭代数组,我建议您从数组的末尾开始,然后迭代到顶部。我无法从inputList中删除任何内容,因为我使用了Arrays.asList来拆分用户输入,我猜该列表是不可修改的
Set<String> duplicates = new HashSet<>(); // sets are unique
for (Iterator<String> i = inputList.iterator(); i.hasNext();)
if (!duplicates.add(i.next())) // add returns true if the set changed
i.remove(); // removes current item