如果对象存在于数组列表中则增加计数,否则将对象添加到数组列表中

Increment count if the object exists in arraylist, else Add the object into arraylist

我从文本文件中读取单词,然后为每个单词创建一个新的 Word 对象并将对象存储到 ArrayList 中。单词的文本作为参数传递到对象中。我已经重写了单词 class 的 equals(Object)hashCode() 方法,以根据单词的文本而不是对象内存位置来检查对象是否相等。我正在尝试将 ArrayList 中的所有唯一单词存储为唯一对象,如果单词在文本文件中重复,则增加单词对象的出现次数。

Scanner file = new Scanner(new File(textfile));
ArrayList<Word> words = new ArrayList<Word>();
while (file.hasNext()) {

    Word w = new Word(fileWord);

    if (words.contains(w)) {
        w.increaseCount();
    } else {
        words.add(w);
    }
}

单词Class是;

public class Word {
    private String text;
    private int count;

    public Word(String wordText) {
        text = wordText;
    }

    public void increaseCount() {
        count += 1;
    }

    @Override
    public boolean equals(Object wordToCompare) {
        if (wordToCompare instanceof Word) {
            Word castedWord = (Word) wordToCompare;
            if (castedWord.text.equals(this.text)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        return text.hashCode();
    }
}

独特的词被添加到 ArrayList,但我的计数没有增加。如何增加计数

问题是您在循环中创建了新的 Word 实例。 当数组包含新创建的 Word 时,您会增加它的计数,而不是之前已经添加到数组中的现有实例。 题目考虑用Map,key是word,value是count。

package example.Whosebug;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WordCount {

    public static void main(String[] args) {
        List<String> sourceList = Arrays.asList("ABC", "XYZ", "HGK", "ABC", "PWT", "HGK", "ABC");

        Map<String, Integer> wordCount = new HashMap();

        for (String word : sourceList) {
            if (wordCount.get(word) != null) {
                wordCount.put(word, wordCount.get(word) +1);
            } else {
                wordCount.put(word, 1);
            }
        }

        System.out.println(wordCount);//output: {ABC=3, XYZ=1, PWT=1, HGK=2}
    }

}

问题出在您的代码中的这条语句上;

while (file.hasNext()) {

    Word w = new Word(fileWord);

    if (words.contains(w)) {
        w.increaseCount(); // Here's what goes wrong.
    } else {
        words.add(w);
    }
}

您正在对新创建的对象调用函数 increaseCount(),该对象将在下一次迭代中被替换,并且您丢失了引用。但实际对象在 ArrayList 中,您应该增加该对象的值。所以,我想说,你的代码应该这样改变;

Scanner file = new Scanner(new File(textfile));
ArrayList<Word> words = new ArrayList<Word>();
while (file.hasNext()) {

    Word w = new Word(fileWord);

    if (words.contains(w)) {
        words.get(words.indexOf(w)).increaseCount(); // Note the change here.
    } else {
        w.increaseCount(); // This is for the first occurrence as 'count' is 0 initially.
        words.add(w);
    }
}

检查这个答案:

    Scanner file = new Scanner(new File(textfile));
    ArrayList<Word> words = new ArrayList<Word>();
    while (file.hasNext()) {

        Word w = new Word(fileWord);

        if (words.contains(w)) {
            w.increaseCount();
            int index = words.indexOf(w);
            Word w1 = words.get(index);
            w1.increaseCount();
            words.set(index, w1);
        } else {
            words.add(w);
        }
    }