Skip to content

Commit 8a73e04

Browse files
committed
Exercise 12 ✔
1 parent 1da6aca commit 8a73e04

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package com.viktorvoltz;
2+
3+
import java.util.*;
4+
5+
public class Exercise12<K,V> {
6+
private Object[][] pairs;
7+
private int index;
8+
public Exercise12(int length) {
9+
pairs = new Object[length][2];
10+
}
11+
public void put(K key, V value) {
12+
if(index >= pairs.length)
13+
throw new ArrayIndexOutOfBoundsException();
14+
pairs[index++] = new Object[] { key, value };
15+
}
16+
@SuppressWarnings("unchecked")
17+
public V get(K key) {
18+
for(int i = 0; i < index; i++)
19+
if(key.equals(pairs[i][0]))
20+
return (V)pairs[i][1];
21+
return null; // Did not find key
22+
}
23+
public String toString() {
24+
StringBuilder result = new StringBuilder();
25+
for(int i = 0; i < index; i++) {
26+
result.append(pairs[i][0].toString());
27+
result.append(" : ");
28+
result.append(pairs[i][1].toString());
29+
if(i < index - 1)
30+
result.append("\n");
31+
}
32+
return result.toString();
33+
}
34+
public static void main(String[] args) {
35+
Exercise12<String,String> map =
36+
new Exercise12<String,String>(6);
37+
map.put("sky", "blue");
38+
map.put("grass", "green");
39+
map.put("ocean", "dancing");
40+
map.put("tree", "tall");
41+
map.put("earth", "brown");
42+
map.put("sun", "warm");
43+
try {
44+
map.put("extra", "object"); // Past the end
45+
} catch(ArrayIndexOutOfBoundsException e) {
46+
System.out.println("Too many objects");
47+
}
48+
System.out.println(map);
49+
System.out.println(map.get("ocean"));
50+
HashMap<String,String> hashMap =
51+
new HashMap<String,String>(6);
52+
hashMap.put("sky", "blue");
53+
hashMap.put("grass", "green");
54+
hashMap.put("ocean", "dancing");
55+
hashMap.put("tree", "tall");
56+
hashMap.put("earth", "brown");
57+
hashMap.put("sun", "warm");
58+
try {
59+
hashMap.put("extra", "object"); // Past the end
60+
} catch(ArrayIndexOutOfBoundsException e) {
61+
System.out.println("Too many objects");
62+
}
63+
System.out.println(hashMap);
64+
System.out.println(hashMap.get("ocean"));
65+
TreeMap<String,String> treeMap =
66+
new TreeMap<String,String>();
67+
treeMap.put("sky", "blue");
68+
treeMap.put("grass", "green");
69+
treeMap.put("ocean", "dancing");
70+
treeMap.put("tree", "tall");
71+
treeMap.put("earth", "brown");
72+
treeMap.put("sun", "warm");
73+
try {
74+
treeMap.put("extra", "object"); // Past the end
75+
} catch(ArrayIndexOutOfBoundsException e) {
76+
System.out.println("Too many objects");
77+
}
78+
System.out.println(treeMap);
79+
System.out.println(treeMap.get("ocean"));
80+
LinkedHashMap<String,String> linkedHashMap =
81+
new LinkedHashMap<String,String>(6);
82+
linkedHashMap.put("sky", "blue");
83+
linkedHashMap.put("grass", "green");
84+
linkedHashMap.put("ocean", "dancing");
85+
linkedHashMap.put("tree", "tall");
86+
linkedHashMap.put("earth", "brown");
87+
linkedHashMap.put("sun", "warm");
88+
try {
89+
linkedHashMap.put("extra", "object"); // Too far
90+
} catch(ArrayIndexOutOfBoundsException e) {
91+
System.out.println("Too many objects");
92+
}
93+
System.out.println(linkedHashMap);
94+
System.out.println(linkedHashMap.get("ocean"));
95+
}
96+
}

0 commit comments

Comments
 (0)