-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssociativeArray.java
More file actions
61 lines (53 loc) · 1.5 KB
/
Copy pathAssociativeArray.java
File metadata and controls
61 lines (53 loc) · 1.5 KB
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
package chapter17.eight;
import java.util.HashMap;
public class AssociativeArray<K,V>{
private Object[][] pairs; //以二维数组模仿map,创建的map将会不可变长度。实际用的是内部类节点Node<K,V>
private int index;
public AssociativeArray(int length) { //传入要创建map的长度。
// TODO Auto-generated constructor stub
pairs=new Object[length][2]; //列数为2 [][0]为键,[][1]为值
}
//模仿map的put方法
public void put(K key,V value){
if(index>=pairs.length){
throw new ArrayIndexOutOfBoundsException();
}
pairs[index++]=new Object[]{key,value};
}
//模仿map的get方法
public V get(K key){
for (int i = 0; i < index; i++) {
if(key==pairs[i][0]){
return (V) pairs[i][1];
}
}
return null;
}
@Override
public String toString() {
// TODO Auto-generated method stub
StringBuilder result=new StringBuilder();
for (int i = 0; i < index; i++) {
result.append(pairs[i][0].toString());
result.append(" ");
result.append(pairs[i][1].toString());
if(i<index-1) //没到最后一行之前,添加换行符
result.append("\n");
}
return result.toString();
}
public static void main(String[] args) {
AssociativeArray<String, String> map=new AssociativeArray<String, String>(3);
map.put("sky", "blue");
map.put("grass", "green");
map.put("ocean", "dancing");
try {
map.put("out of index", "exception");
} catch (Exception e) {
// TODO: handle exception
System.out.println("too many Objects");
}
System.out.println(map);
System.out.println(map.get("sky"));
}
}