-
Notifications
You must be signed in to change notification settings - Fork 77
/
ArrayMap.java
78 lines (64 loc) · 1.33 KB
/
ArrayMap.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
71
72
73
74
75
76
77
78
import java.util.List;
import java.util.Arrays;
import java.util.Iterator;
public class ArrayMap<K, V> implements Iterable<K> {
private K[] keys;
private V[] values;
private int size;
public ArrayMap() {
keys = (K[]) new Object[100];
values = (V[]) new Object[100];
size = 0;
}
private int findKey(K key) {
for (int i = 0; i < size; i += 1) {
if (keys[i].equals(key)) {
return i;
}
}
return -1;
}
public void put(K key, V value) {
int i = findKey(key);
if (i > -1) {
values[i] = value;
return;
}
keys[size] = key;
values[size] = value;
size += 1;
}
public V get(K key) {
if (findKey(key) < 0) {
throw new IllegalArgumentException(key + " was not a valid key.");
}
return values[findKey(key)];
}
public boolean containsKey(K key) {
int i = findKey(key);
return (i > -1);
}
public List<K> keys() {
if (size == 0) {
return null;
}
return Arrays.asList(keys).subList(0, size);
}
public class MapWizard implements Iterator<K> {
private int notionOfWhereHeIs;
public MapWizard() {
notionOfWhereHeIs = 0;
}
public boolean hasNext() {
return (notionOfWhereHeIs < size);
}
public K next() {
K currentThing = keys[notionOfWhereHeIs];
notionOfWhereHeIs += 1;
return currentThing;
}
}
public Iterator<K> iterator() {
return new MapWizard();
}
}