1 package text; 2 3 public class SxtMap001{ 4 SxtEntry[] arr = new SxtEntry[990]; 5 int size; 6 public void put(Object key,Object value){ 7 SxtEntry e = new SxtEntry(key,value); 8 arr[size++]=e;//放入数组中 9 } 10 11 public Object get(Object key){ 12 for(int i=0;i<size;i++){ 13 if(arr[i].key.equals(key)){ 14 return arr[i].value; 15 } 16 } 17 return null; 18 } 19 public boolean containsKey(Object key){ 20 for(int i=0;i<size;i++){ 21 if(arr[i].key.equals(key)){ 22 return true; 23 } 24 } 25 return false; 26 } 27 28 public boolean containsvalue(Object value){ 29 for(int i=0;i<size;i++){ 30 if(arr[i].value.equals(value)){ 31 return true; 32 } 33 } 34 return false; 35 } 36 public void putAll(SxtMap001 T){ 37 for(int i=0;i<T.size;i++){ 38 System.out.println(arr[i].key+":"+((Wife)arr[i].value).name); 39 } 40 } 41 42 43 public static void main(String[] args){ 44 SxtMap001 m = new SxtMap001(); 45 m.put("张三",new Wife("杨幂"));//Wife请参考上一篇,其实就是一个用来创建对象的类 46 m.put("张三",new Wife("李四")); 47 Wife w=(Wife) m.get("张三"); 48 System.out.println(w.name); 49 Object M="李四"; 50 System.out.println(m.containsKey("张三")); 51 System.out.println(m.containsvalue("李四")); 52 m.putAll(m); 53 54 55 } 56 } 57 class SxtEntry{ 58 Object key; 59 Object value; 60 61 public SxtEntry(Object key,Object value){ 62 super(); 63 this.key=key; 64 this.value=value; 65 } 66 }
运行结果: 杨幂 true false 张三:杨幂 张三:李四