-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayIndex.java
More file actions
93 lines (83 loc) · 2.54 KB
/
Copy pathArrayIndex.java
File metadata and controls
93 lines (83 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.util.*;
import java.io.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
/**
* Sorted ArrayList Index Implementation
*
* @author Tafita Rakotozandry & Taylor Strong & Khalid Al-Motaery
* @version 11/28/2020
*/
public class ArrayIndex extends IndexAbstract implements Index
{
private static ArrayList<Entry> theArray;
private static ArrayList<String> arrayOfWords;
Comparator<Entry> c;
/**
* Constructor for objects of class SortedArray
*/
public ArrayIndex()
{
theArray = new ArrayList<Entry>();
arrayOfWords = new ArrayList<String>();
c = new Comparator<Entry>()
{
public int compare(Entry e1, Entry e2){
return e1.getWord().compareTo(e2.getWord());
}
};
}
/**
* Adds a word to the ArrayList
*
* @param w the word that is being added to the ArrayList
* @param k the line that the word is on
* @return true or false depending on if the addition was successful or not
*/
public boolean add(String w, Integer k)
{
String word = w.toLowerCase();
if (!isEnglish(word)) {
return false;
}
if (theArray.size() == 0){
Entry newEntry = new Entry(word, k);
theArray.add(newEntry);
arrayOfWords.add(word);
return true;
}
else{
int index = Collections.binarySearch(arrayOfWords, word);
if (index < 0){
Entry newEntry = new Entry(word, k);
theArray.add(-index - 1, newEntry);
arrayOfWords.add(-index - 1, word);
return true;
}
else {
return theArray.get(index).addLine(k);
}
}
}
/**
* Creates and writes the output file
*/
public void createOutFile(){
try{
//create the output file
File outputFile = new File("ArrayOutput.txt");
outputFile.createNewFile();
PrintWriter writeFile = new PrintWriter(outputFile);
Iterator <Entry> itr = theArray.iterator();
Entry entry = null;
while (itr.hasNext()) {
entry = itr.next();
writeFile.println(entry.getWord() + " " + entry.getLines());
}
writeFile.close();
} catch (Exception e) {
System.out.println("An error has occurred in createOutFile: " + e);
}
}
}