Map练习题

1:定义一个类,类里面有一个属性col,类型是集合类型 Collection,实现下列方法:可以向col 里面添加数据、修改数据、查询数据、删除数据。也就是把这个 col当作一个数据存储的容器,对其实现数据的增删改查的方法。

View Code
 1 import java.util.List;
2 import java.util.ArrayList;
3 import java.util.Iterator;
4 class Col
5 {
6 private List<String> ls;
7 public Col()
8 {
9 this.ls=new ArrayList<String>();
10 }
11 public List<String> getCol()
12 {
13 return this.ls;
14 }
15 public void add(String s)
16 {
17 this.ls.add(s);
18 }
19 public boolean search(String name)
20 {
21 if(this.ls.indexOf(name)!=-1)
22 {
23 return true;
24 }
25 else
26 {
27 return false;
28 }
29 }
30 public void delete(int x)
31 {
32 if(x>=0&&x<this.ls.size())
33 {
34 this.ls.remove(x);
35 }
36 }
37 public void update(int y,String s)
38 {
39 if(y>=0&&y<this.ls.size())
40 {
41 this.ls.set(y,s);
42 }
43 }
44 }
45 public class ListTest02
46 {
47 public static void main(String[] args)
48 {
49 Col cl=new Col();
50 cl.add("ab");
51 cl.add("egher");
52 cl.add("3454yngfhb");
53 cl.add("dfe247fe");
54 cl.add("hg4re");
55 cl.add("!~545df");
56 cl.add("absdgh5 4ythbdf");
57 Iterator<String> it=cl.getCol().iterator();
58 while(it.hasNext())
59 {
60 System.out.print(it.next()+",");
61 }
62 System.out.print("\n");
63 System.out.println(cl.search("hg4re"));
64 cl.delete(2);
65 for(String s:cl.getCol())
66 System.out.print(s+" ");
67 System.out.print("\n");
68 cl.update(0,"fffffffffffffff");
69 for(String s:cl.getCol())
70 System.out.print(s+" ");
71
72 }
73 }

2:把上题的Collection改成使用Map实现

View Code
 1 import java.util.Map;
2 import java.util.Set;
3 import java.util.Iterator;
4 import java.util.HashMap;
5 class MyMap
6 {
7 private Map<String,Integer> myMp;
8 public MyMap()
9 {
10 this.myMp=new HashMap<String,Integer>();
11 }
12 public void add(String name,int age)
13 {
14 this.myMp.put(name,age);
15 }
16 public boolean search(String s)
17 {
18 boolean bl=false;
19 Set<String> st=this.myMp.keySet();
20 Iterator it=st.iterator();
21 while(it.hasNext())
22 {
23 if(it.next().equals(s))
24 {
25 bl=true;
26 }
27 }
28 return bl;
29 }
30 public void print()
31 {
32 for(Map.Entry<String,Integer> me:this.myMp.entrySet())
33 System.out.print("姓名:"+me.getKey()+" 年龄:"+me.getValue()+"\n");
34 }
35 public void delete(String name)
36 {
37 this.myMp.remove(name);
38 }
39 public void update(String s,int in)
40 {
41 if(this.search(s))
42 {
43 //this.myMp.get(s);
44 this.myMp.put(s,in);
45 }
46 else
47 {
48 this.add(s,in);
49 }
50 }
51
52 }
53 public class MapTest
54 {
55 public static void main(String[] args)
56 {
57 MyMap mm=new MyMap();
58 mm.add("张三",30);
59 mm.add("李四",20);
60 mm.add("王麻子",30);
61 mm.add("不知道",40);
62 mm.add("孙七",60);
63 mm.add("赵六",70);
64 mm.add("钱八",80);
65 mm.add("李瘸",90);
66 mm.add("丝袜",230);
67 // mm.print();
68 // System.out.println(mm.search("张"));
69 // mm.delete("不知道");
70 // mm.print();
71 // System.out.println(mm.search("张三"));
72 // mm.update("张三",60);
73 // mm.print();
74 mm.update("龙五",90);
75 mm.print();
76 }
77 }





转载于:https://www.cnblogs.com/xiongyu/archive/2011/11/30/2268634.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值