-
Notifications
You must be signed in to change notification settings - Fork 1
/
lab7_ script.txt
105 lines (61 loc) · 1.3 KB
/
lab7_ script.txt
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
public class Map<Key, Value> {
private Key [] Keys;
private Value [] Values;
private int count;
public Map(int length) {
if(length<0){
throw new IllegalArgumentException("invalid length");
}
Keys = (Key[])new Object[length];
Values = (Value[])new Object[length];
count =0;
}
public Value get(Key key) {
for(int index=0; index<count; index++)
{
if(isEqual(Keys[index],key))
return Values[index];
}
throw new IllegalArgumentException();
}
private boolean isEqual(Key leftKey, Key rightKey) {
if(leftKey!=null && rightKey !=null) {
return leftKey.equals(rightKey);
}
return (leftKey==rightKey);
}
public boolean isIn(Key key)
{
for(int index=0; index<count; index++)
{
if(isEqual(Keys[index],key))
{
return true;
}
}
return false;
}
public void put(Key key, Value value)
{
int index = where(key);
if(index==-1)
{
if (count == Keys.length)
throw new IllegalStateException("map is full");
}
else
{
Keys[count]=key;
Values[count] = value;
count++;
}
}
private int where(Key key) {
for(int index=0; index<count; index++) {
if(isEqual(this.Keys[index],key)) {
return index;
}
}
return -1;
}
}