首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何统计文本文件中唯一词的个数?不允许使用哈希

问如何统计文本文件中唯一词的个数?不允许使用哈希
EN

Stack Overflow用户
提问于 2014-03-12 09:01:37
回答 1查看 1.2K关注 0票数 0

我需要创建一个程序来计算文件中的单词数量和唯一单词的数量……我已经想出了如何计算单词的数量,但我在计算唯一单词部分时遇到了麻烦。这是我的统计总字数的方法。

代码语言:javascript
复制
public static int numberWord(File f) throws FileNotFoundException{
    Scanner welcome = new Scanner(f);
    int count=0;
    while(welcome.hasNext()){
        String word=welcome.next();
        count++;
    }
    return count;
EN

回答 1

Stack Overflow用户

发布于 2014-03-12 09:13:00

嗯,这是一项荒谬的任务,而且通常是一种糟糕的实现任务的方式。

无论如何,这里有一些伪代码和一个简单的方法定义,可以用来解决任务-它有可怕的复杂性界限和固定的限制,但它应该足以开始一个。

代码语言:javascript
复制
// Find the index of the word within an array.
// If the word currently is not in the array, then
// add it and return the index the word was added at.
int ensureWord(String[] words, String word) {
    for each index in words do
        if the word is equal() to the word at the index then
            // existing word found
            return the index
        if (null) is at the index then
            // "found the end" of the known words, so must be a new word
            assign word to the words array at the index
            return the index

    throw terrible exception, word not ensured
}

String words[] = new String[MAX_DISTINCT_WORDS_ALLOWED];
String counts[] = new int[MAX_DISTINCT_WORDS_ALLOWED];

for each word in the file do
   find/ensure the index in words by invoking ensureWord(words, word)
   and increment the count at the index by one
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22339946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档