2

我正在尝试做出以下声明: InvertedFile.invertedFile.add(d.getAccessionNumber(), d.getWords());

它应该创建一个新的 reverseList 对象并将其添加到链表(invertedfile)中。我收到以下错误:

发现 2 个错误:文件:C:\Users\Name\Database.java [行:21] 错误:C:\Users\Name\Database.java:21:找不到符号符号:方法 add(int,WordList) 位置:类 java.util.LinkedList 文件:C:\Users\Name\InvertedFile.java [行:39]
错误:C:\Users\Name\InvertedFile.java:39:找不到符号符号:方法 add(int,java. lang.String) 位置:类 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!!
4

1 回答 1

3

你的静态invertedFile 变量是一个LinkedList,你试图在它上面调用一个InvertedFile 方法,这是没有意义的。您只能在 LinkedList 变量上调用 LinkedList 可用的方法(检查 API)。

我认为您的错误是使变量 reverseFile 静态。使其成为私有实例变量,并让任何需要使用 InvertedFile 的类创建该类的实例并调用实例上的方法。

于 2013-03-10T00:52:53.887 回答