我正在尝试发表以下声明:InvertedFile.invertedFile.add(d.getAccessionNumber(),d.getWords());
应该创建一个新的invertedList对象并将其添加到链表(invertfile)。我收到以下错误:
找到2个错误:文件:C:\ Users \ Name \ Database.java [line:21]错误:C:\ Users \ Name \ Database.java:21:找不到符号符号:方法add(int,WordList )location:类java.util.LinkedList文件:C:\ Users \ Name \ InvertedFile.java [line:39]
错误:C:\ Users \ Name \ InvertedFile.java:39:找不到符号符号:方法add(int,java.lang.String)location:class java.util.LinkedList 错误的位置:
public int add( Document d ){
int accessNumber = d.getAccessionNumber();
//Stores document text in InvertedFile data structure
InvertedFile.invertedFile.add(d.getAccessionNumber(), d.getWords());
//Adds document to accessNumber index of LinkedList of documents
if (documentList.size()<accessNumber){
documentList.add(accessNumber,d);
docNumber++;
return accessNumber;
}
else return -1;
}
反转文件类
import java.util.*;
public class InvertedFile implements InvertedFileInterface {
public static LinkedList<InvertedList> invertedFile = new LinkedList<InvertedList>();
public InvertedFile(){
}
//Takes in docAccessionNumber and WordList and updates/creates invertedList files for each word in wordlist
public boolean add(int docNumber, WordList w){
String words = w.toString();
StringTokenizer parseWords = new StringTokenizer(words);
//Checks to see if InvertedFile already contains invertedLists for the given words, if so adds docID to existing lists
while (parseWords.hasMoreTokens()){
String checkWord = parseWords.nextToken();
for (int k = 0; k<invertedFile.size()-1; k++){
InvertedList temp = invertedFile.get(k);
if (checkWord.equals(temp.getWord())){
temp.add(docNumber);
}
}
}
//Checks to see if invertedFile contains lists for given words, if not, makes a new one and adds docNumber to the list
String wordsToCheck = w.toString();
StringTokenizer processWords = new StringTokenizer(wordsToCheck);
while (processWords.hasMoreTokens()){
String word = processWords.nextToken();
Boolean notFound = true;
for (int k = 0; k<invertedFile.size()-1; k++){
if (word.equals(invertedFile.get(k).getWord())){
notFound = false;
}
}
if (notFound){
InvertedFile.invertedFile.add(docNumber, word);
}
}
Database.docNumber++;
return true;
}
public int numLists(){
return invertedFile.size();
}
public int numTokens(){
return invertedFile.size();
}
public InvertedList getInvertedList( String word ){
for (int k = 0; k<invertedFile.size()-1; k++){
if (word.equals(invertedFile.get(k).getWord())){
return invertedFile.get(k);
}
}
倒置列表类:
import java.util.*;
public class InvertedList implements InvertedListInterface {
public static LinkedList<Integer> documentsContainingWord = new LinkedList<Integer>();
public String targetWord;
public InvertedList( String word ){
String targetWord = word;
//Scans documents in database for word
for (int j = 1; j<=Database.documentList.size(); j++){
Document temp = Database.documentList.get(j);
WordList words = temp.getWords();
//If document wordList contains word, and LinkedList doesn't already have that doc listed, add to LinkedList
//Already sorted since processed from 1 to size
if (words.contains(word) && !documentsContainingWord.contains(temp.getAccessionNumber())){
documentsContainingWord.add(temp.getAccessionNumber());
}
}
}
//Creator method
public InvertedList(int docNumber, String word){
String targetWord = word;
documentsContainingWord.add(docNumber);
}
public String getWord(){
return targetWord;
}
//Adds docNumber to sorted position of LinkedList documentsContainingWord
public boolean add( int docNumber ){
//If document already in list, don't add
if (!documentsContainingWord.contains(docNumber)){
//Places number at sorted position on list
//If document should be placed at index 0 (has smallest accessionNumber)
if (docNumber<documentsContainingWord.get(0)){
return documentsContainingWord.offerFirst(docNumber);
}
//If document should be placed in middle
if (docNumber<documentsContainingWord.get(documentsContainingWord.size()) && docNumber>documentsContainingWord.get(0)){
int temp = 1;
int index = 0;
while (temp<docNumber){
temp = documentsContainingWord.get(index);
index++;
}
documentsContainingWord.add(index+1, docNumber);
return true;
}
//If document should be placed at end
if (docNumber>documentsContainingWord.get(documentsContainingWord.size())){
return documentsContainingWord.offerLast(docNumber);
}
//Else document number already in inverted list
else {
return false;
}
}
return false;
}
public LinkedList getList(){
return documentsContainingWord;
}
Thank you!!
答案 0 :(得分:3)
你的static invertedFile变量是一个LinkedList,你试图在它上面调用一个InvertedFile方法,这是没有意义的。您只能在LinkedList变量上调用LinkedList可用的方法(检查API)。
我认为你的错误是使变量invertFile变为静态。使它成为私有实例变量,并使任何需要使用InvertedFile的类创建该类的实例并在该实例上调用方法。