-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayDictionary1.java
More file actions
172 lines (158 loc) · 4.44 KB
/
ArrayDictionary1.java
File metadata and controls
172 lines (158 loc) · 4.44 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import java.util.*;
import java.io.*;
public class ArrayDictionary1 {
public static String[] Dict;
public int numOfElements;
/* GENERAL METHODS hand-in 1*/
public ArrayDictionary1() {
Dict = new String[30000];
numOfElements = 0;
}
/* GENERAL METHODS Hand-in 1*/
/**
* Populate the essential dictionary data structure
* with all the words read from the input language.
* Returns nothing.
*
* @param filepath the filepath for the input language.
* @throws IOException catch an file errors.
*/
public void CreateDictionary(String filepath)throws IOException{
// todo
// In file = new In(filepath);
Scanner file = new Scanner(new File(filepath));
while (file.hasNextLine()) {
String word = file.nextLine();
if(!(isWord(word))){
Dict[numOfElements++] = word;
}
}
}
/**
* Look for the word in the dictionary
*
* @param word the word to look for in the dictionary.
* @return the return value indicating whether word is
* found or not.
*/
public boolean isWord(String word) {
for (int i = 0; i < numOfElements; i++) {
if (Dict[i].equals(word)) {
return true;
}
}
return false;
}
/**
* adds a new word to the dictionary if the word
* not found in the dictionary.
*
* @param word word to add in the dictionary
*/
public void addNewWord(String word) {
// for (int i = 0; i < Dict.length; i++) {
// if (!Dict[i].contains(word)) {
//
// int wordIndex = locatePosition();
// Dict[wordIndex] = word;
//
// }
// }
if (!isWord(word)) {
Dict[numOfElements++] = word;
}
}
/**
* Removes the word in the dictionary if the word is there
* in the dictionary.
*
* @param word the word to remove
*/
public void removeWord(String word) {
// //todo
// String[] dict2 = new String[Dict.length - 1];
// int wordIndex = 0;
// for (int i = 0; i < Dict.length; i++) {
// if (Dict[i] != null && Dict[i].equals(word)) {
// wordIndex = i;
// break;
// }
// }
// System.out.println(wordIndex);
//
// for (int j = 0; j < wordIndex; j++) {
// dict2[j] = Dict[j];
// }
// for (int k = wordIndex + 1; k < dict2.length; k++) {
// dict2[k - 1] = Dict[
// Dict = dict2;k];
//// }
// System.out.print(Arrays.toString(Dict));
int index = 0;
for (int i = 0; i < Dict.length; i++) {
// find the index of the word to be removed
if (Dict[i].equals(word)) {
index = i;
numOfElements--;
}
}
String newDict[] = new String[Dict.length];
int j = 0;
for (int i = 0; i < newDict.length; i++, j++) {
if (Dict[i].equals(word)) {
continue;
}
newDict[j] = Dict[i];
}
for (int i = 0; i < newDict.length; i++) {
Dict[i] = newDict[i];
}
}
/**
* adds all the words in the dictionary to an arraylist.
*
* @return the arraylist of all words in the dictionary.
*/
public ArrayList<String> getDictionaryWords() {
//todo
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < numOfElements; i++) {
// while (!(Dict[i] == null)) {
// System.out.println(Dict[i] + "\n");
list.add(Dict[i]);
// }
}
return list;
}
// private String get(String s) {
// return s;
// }
/**
* @return the number of elements in the dictionary.
*/
public int getNumberOfElements() {
// //todo
// int count = 0;
// for (int i = 0; i < Dict.length; ++i) {
// while (Dict[i] != null) {
// count++;
// }
// numOfElements = count - 1;
// }
return numOfElements;
}
/**
* find the position where to add word.
*
* @return the integer position value
*/
// private int locatePosition() {
// int position = 0;
// while (position <= numOfElements) {
// position++;
// }
// return position;
//
//
// }
}