Java 涉及数组的类问题

Java 涉及数组的类问题,java,class,search,for-loop,Java,Class,Search,For Loop,我试图创建一个WordCounts数组,然后使用split方法逐行遍历一个文件,将其分离为标记。然后,对于每个标记,如果它在wordList中,则递增count,如果它不在wordList中,则只需将其添加到列表中即可 Hmwkclass- public class Hmwk { public static void main(String[] args) throws FileNotFoundException { int n=0; Wo

我试图创建一个
WordCount
s数组,然后使用split方法逐行遍历一个文件,将其分离为标记。然后,对于每个标记,如果它在
wordList
中,则递增
count
,如果它不在
wordList
中,则只需将其添加到列表中即可

Hmwk
class-

public class Hmwk {        
    public static void main(String[] args) throws FileNotFoundException {
        int n=0;
        WordCount[] wordList= new WordCount[10000];
        Scanner words = new Scanner(new File("input.txt"));
        while (words.hasNextLine() && n < 10000) {
            String line = words.nextLine();
            String[] tokens = line.split("[^\\p{Alpha}]");
            for (int i = 0; i < tokens.length; i++) {
                if (tokens[i].length() > 0) {
                    WordCount word = new WordCount(tokens[i]);
                    int foundAt = search(wordList, word, n);
                    if (foundAt >= 0) {
                        word.increment();
                    } else {
                        wordList[n]=word;
                        n++;
                    }
                }
            }
        }
        //Arrays.sort(wordList);
        String alphabeticFileName = "alphabetic.txt";
        String frequencyFilename = "frequency.txt";
        PrintWriter output = new PrintWriter(alphabeticFileName);
        for (int i=0; i < n;i++) {
            output.println(wordList[i].toString());
        }
        output.close();
        //Sort on frequency somehow
        PrintWriter output2 = new PrintWriter(frequencyFilename);
        for (int i=0; i < n; i++) {
            output2.println(wordList[i].toString());
        }
        output2.close();

    }

    public static int search(WordCount[] list,WordCount word, int n) {
        int result = -1;
        int i=0;
        while (result < 0 && i < n) {
            if (word == list[i]) {
                result = i;
            }
            i++;
        }
        return result;
    }        
}
class WordCount {        
    String word;
    int count;
    static boolean compareByWord;

    public WordCount(String aWord) {
        setWord(aWord);
        count = 1;
    }

    private void setWord(String theWord) {
        word = theWord;
    }

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

    public static void sortByWord() {
        compareByWord = true;
    }

    public static void sortByCount() {
        compareByWord = false;
    }

    public String toString() {
        String result = String.format("%s (%d)", word, count);
        return result;
    }        
}
它编译和运行都很好,但出于某种原因

Peter (1) Piper (1) picked (1) a (1) peck (1) of (1) pickled (1) peppers (1) A (1) peck (1) of (1) pickled (1) peppers (1) Peter (1) Piper (1) picked (1) If (1) Peter (1) Piper (1) picked (1) a (1) peck (1) of (1) pickled (1) peppers (1) Where (1) s (1) the (1) peck (1) of (1) pickled (1) peppers (1) that (1) Peter (1) Piper (1) picked (1) 彼得(1) 风笛手(1) 挑选(1) a(1) 佩克(1) 第(1)款 腌制(1) 辣椒(1) A(1) 佩克(1) 第(1)款 腌制(1) 辣椒(1) 彼得(1) 风笛手(1) 挑选(1) 如果(1) 彼得(1) 风笛手(1) 挑选(1) a(1) 佩克(1) 第(1)款 腌制(1) 辣椒(1) 其中(1) s(1) 第(1)条 佩克(1) 第(1)款 腌制(1) 辣椒(1) 第(1)款 彼得(1) 风笛手(1) 挑选(1)
作为输出。我的类或搜索方法有问题吗?我迷路了,非常感谢您的任何帮助。

如果不仔细阅读您的代码,问题似乎就在这里:

        if (foundAt >= 0)
        {
            word.increment();
        }
在这里,您将增加“新”单词,而不是先前添加的单词。 应该是这样的:

        if (foundAt >= 0)
        {
            wordList[foundAt].increment();
        }

您可以按如下方式更改
search
方法的签名-

public static WordCount search(WordCount[] list, String word)
WordCount wordCount = search(wordList, tokens[i]);
您只需要传递数组和当前标记(单词或字符串),该方法应返回单词的
WordCount
,如果找不到,则返回null。这样,您就不需要处理索引,也不需要为当前单词创建
WordCount
的实例(如果它已经在数组中)

第一,你的
搜索方法中的bug是
word==list[i]
。这不是检查对象相等性的方式,相反,您应该为此使用该方法

现在,在更改
search
方法的签名之后,在该方法中,您将循环通过
list
数组,将每个
WordCount
(即
list[i]
)中的单词与当前数组元素
标记[i]
,如果它们相等,则立即返回当前
WordCount
(即
列表[i]

然后您将调用
search
方法,如下所示-

public static WordCount search(WordCount[] list, String word)
WordCount wordCount = search(wordList, tokens[i]);
然后立即检查
wordCount
是否为空。如果为空,则为当前单词创建
wordCount
的新实例(即
tokens[i]
)并将其放入数组中。如果不为空,则只需增加其计数(
wordCount.increment()