-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordGram.java
69 lines (61 loc) · 1.47 KB
/
WordGram.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
public class WordGram
{
private String[] myWords;
private int myHash;
public WordGram(String[] source, int start, int size)
{
myWords = new String[size];
System.arraycopy(source, start, myWords, 0, size);
}
public String wordAt(int index)
{
if (index < 0 || index >= myWords.length)
{
throw new IndexOutOfBoundsException("bad index in wordAt "+index);
}
return myWords[index];
}
public int length()
{
return myWords.length;
}
public String toString()
{
String ret = "";
for(int i=0;i<myWords.length;i++)
{
ret +=myWords[i]+" ";
}
return ret.trim();
}
public boolean equals(Object o)
{
WordGram other = (WordGram) o;
if(this.length() != other.length())
{
return false;
}
for(int i=0;i<myWords.length;i++)
{
if(!(myWords[i].equals(other.myWords[i])))
{
return false;
}
}
return true;
}
public WordGram shiftAdd(String word)
{
WordGram out = new WordGram(myWords, 0, myWords.length);
for(int i=0; i<out.myWords.length-1;i++)
{
out.myWords[i] = out.myWords[i+1];
}
out.myWords[myWords.length-1]=word;
return out;
}
public int hashCode ()
{
return toString().hashCode();
}
}