涉及数组的类问题 [英] Class Issues involving an Array

查看:164
本文介绍了涉及数组的类问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个 WordCount 的数组,然后通过使用split方法将文件行分隔为令牌来遍历文件行。然后对于每个令牌,如果它在 wordList 中,增加 count ,如果不在 wordList 只是简单地将它添加到列表中。

I'm attempting to make an array of WordCounts, then iterate through a file line by line separating it into tokens using a split method. Then for each token, if it's in wordList, increment count, and if it's not in wordList just simply add it to the list.

Hmwk class -

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

WordCount class -

WordCount class -

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

它编译并运行正常,但由于某种原因给予

It compiles and runs fine, but for some reason I'm given


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)

作为输出。我的班有什么问题,或我的搜索方法在这里?

as output. Is there something wrong with my class, or my search method here? I'm lost, any and all help is much appreciated.

推荐答案

您可以更改搜索方法的签名如下 -

You can change the search method's signature as follows -

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

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

You only need to pass the array and the current token (word or string), and the method should return the WordCount for the word or null if not found. That way you don't need to deal with the index, and don't need to create an instance of WordCount for current word if it's already in the array.

其中,搜索方法中的错误是 word == list [i] 。这不是如何检查对象的平等,而应该使用 .equals() 方法。

One, bug in your search method is word == list[i]. That's not how you check object equality, instead you should use the .equals() method for that purpose.

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

Now, after changing search method's signature, within the method, you would loop through the list array, compare the word inside each WordCount (i.e. list[i]) with current array element, tokens[i], and if they are equal then immediately return current WordCount (i.e. list[i]).

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

Then you will call the search method as follows -

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

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

Then immediately check if wordCount is null. If it's null then create new instance of WordCount for the current word (i.e. tokens[i]) and put it in the array. And if it's not null then just increment it's count (wordCount.increment()).

这篇关于涉及数组的类问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆