涉及数组的类问题

时间:2014-04-14 23:56:33

标签: java class search for-loop

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

Hmwk课程 -

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;
    }        
}

WordCount课程 -

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)

作为输出。我的班级或我的搜索方法在这里有问题吗?我很失落,非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

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

        if (foundAt >= 0)
        {
            word.increment();
        }

这里增加“新”字而不是之前添加的字。 应该是这样的:

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

答案 1 :(得分:0)

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

public static WordCount search(WordCount[] list, String word)

您只需要传递数组和当前标记(单词或字符串),该方法应返回该单词的WordCount,如果未找到则返回null。这样你就不需要处理索引,如果它已经在数组中,则不需要为当前单词创建WordCount的实例。

其中一个,search方法中的错误是word == list[i]。这不是你检查对象相等性的方式,而是你应该为此目的使用.equals()方法。

现在,在方法中更改search方法的签名后,您将循环遍历list数组,将每个WordCount内的单词(即list[i])与当前数组元素tokens[i],如果它们相等,则立即返回当前WordCount(即list[i])。

然后您将按如下方式调用search方法 -

WordCount wordCount = search(wordList, tokens[i]);

然后立即检查wordCount是否为空。如果它为null,则为当前单词(即WordCount)创建tokens[i]的新实例,并将其放入数组中。如果它不是null,那么只需增加它的计数(wordCount.increment())。