我有一個WordFreq
類,該類有一個方法,該方法從WordCount
類創建一個數組。我有方法訪問WordCount
的其他行沒有問題。從另一個類創建一個數組
我:
public class WordCount{
private String word;
private int count;
public WordCount(String w){
word = w;
count = 0;
}
其次是類方法:
public class WordFreq extends Echo {
String words, file;
String[] wordArray;
WordCount[] search;
WordFreq傳遞一個文本文件(回聲處理)等詞來搜索的字符串。
public WordFreq(String f, String w){
super(f);
words = w;
}
public void processLine(String line){
file = line;
wordArray = file.split(" ");
// here is where I have tried several methods to initialize the search
// array with the words in the words variable, but I can't get the
// compiler to accept any of them.
search = words.split(" ");
StringTokenizer w = new StringTokenizer(words);
search = new WordCount[words.length()];
for(int k =0; k < words.length(); k++){
search[k] = w.nextToken();
我嘗試了其他一些不起作用的東西。我嘗試將search[k]
=右邊的內容轉換爲WordCount
,但它不會超過編譯器。我不斷收到不兼容的類型。
Required: WordCount found: java.lang.String.
我不知道該從哪裏出發。
爲了更好地提供幫助,請發佈[SSCCE](http://sscce.org/)。 –