-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDualArrayList.java
70 lines (67 loc) · 1.71 KB
/
DualArrayList.java
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
import java.util.ArrayList;
public class DualArrayList {
ArrayList<Integer> count = new ArrayList<Integer>();
ArrayList<String> words = new ArrayList<String>();
public DualArrayList() {
}
/*
*
* @param name
* word to be added
*
* searches the words for name. If name is not in words it is added, along with a 1 being added
* in the same index as in words
*
* if it is present add +1 to the index of the word in words to the count of that index
*/
public void add(String name) {
for (String x: words) {
if (x.equals(name.toLowerCase())) {
int index = words.indexOf(x);
count.set(index, count.get(index)+1);
return;
}
}
count.add(1);
words.add(name.toLowerCase());
}
/*
* selection sort algorithm of count. Index is reflected onto words arraylist
*/
public void sort() {
for (int i = 0; i < count.size(); i++) {
int max = count.get(i);
int index = i;
for (int j = i; j < count.size(); j++) {
if (count.get(j) > max) {
index = j;
max = count.get(j);
}
}
String maxWord = words.get(index);
count.set(index, count.get(i));
words.set(index, words.get(i));
count.set(i, max);
words.set(i, maxWord);
}
}
/*
* Sorts to find the highest numbers/most used words
* @returns top 10 and their occurrence count
*/
public String getInfo() {
sort();
if (count.size() == 0) {
return "No occurrences of that word.";
}
String response = "";
int range = count.size();
if (count.size() > 10) {
range = 10;
}
for (int i = 0; i < range; i++) {
response += "\"" + words.get(i) + "\"" + " occurs " + count.get(i) + " times.\n";
}
return response;
}
}