-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.java
54 lines (51 loc) · 1.04 KB
/
Map.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
//maymi001
//Benjamin Maymir
class Map<Key, Value> {
private Key[] keys;
private Value[] values;
private int count;
public Map(int length) {
if(length<0)
throw new IllegalArgumentException();
count = 0;
keys = (Key[]) new Object[length];
values = (Value[]) new Object[length];
}
public Value get(Key key) {
int temp = where(key);
if(temp==-1) {
throw new IllegalArgumentException();
}
return(values[temp]);
}
private boolean isEqual(Key leftKey, Key rightKey) {
if(leftKey == null)
return(leftKey == rightKey);
return(leftKey.equals(rightKey));
}
public boolean isIn(Key key) {
return((where(key)!=-1));
}
public void put(Key key, Value value) {
int temp = where(key);
if(temp!=-1) {
values[temp]=value;
} else {
if(count==keys.length) {
throw new IllegalStateException();
}
keys[count]=key;
values[count]=value;
count++;
}
}
private int where(Key key) {
int index = count;
while(index-->0) {
if(isEqual(key,keys[index])) {
return(index);
}
}
return(-1);
}
}