Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * represents the ArrayList of IndexEntries
- * that gets written to the file
- */
- import java.util.*;
- public class DocumentIndex extends ArrayList<IndexEntry>
- {
- /**constructors - one no args, one where capacity is plugged in*/
- public DocumentIndex()
- {
- super();
- }
- public DocumentIndex(int c)
- {
- super(c);
- }
- /**addWord - two parameters - String word and int num
- 1. Call foundOrInserted method to figure out position of the IndexEntry
- for word, and retrieve that IndexEntry from this DocumentIndex
- 2. adds num to the IndexEntry that was either looked up
- or created in step one, by calling it's add(num) method.
- */
- public void addWord(String w, int n)
- {
- IndexEntry entry = this.get(foundOrInserted(w));
- entry.add(n);
- }
- /**
- * foundOrInserted
- * Tried to find an IndexEntry with a given word in this DocumentIndex.
- * If not found, inserts a new IndexEntry for word in the appropriate
- * place alphabetically. Returns the index of the found or inserted InexEntry.
- * use compareTo
- * example: "CAT.compareTo("AND")>0
- */
- private int foundOrInserted(String word) //returns the line number
- {
- int count=0;
- IndexEntry test = new IndexEntry(word);
- this.add(test);
- DocumentIndex d = new DocumentIndex();
- for(int x=0;x<d.size();x++)
- {
- count++;
- if(d.get(x).getWord().equals(test))
- return count;
- }
- return d.size(); //change this line where it returns
- }
- //for each word in str, calls addWord(word, num)
- public void addAllWords(String str, int num)
- {
- String words[] = str.split("\\W+"); //splits according to whitespace
- for(String w: words)
- {
- if(w.length()>0)
- addWord(w, num);
- //fill in!!!
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement