• 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 }





  • 相关阅读:
    [leetcode] Combination Sum and Combination SumII
    nginx随着passenger构造ruby on rails页
    form 为什么上传文件enctype现场
    ftk学习记录(多形式的文章)
    Android setDisplayOptions 具体的使用说明
    存储结构二叉树
    SQLSERVER存储过程语法的具体解释
    iOS多用连接、反向协议、安全
    struts2于validate要使用
    Oracle存储过程实现返回多个结果集 在构造函数方法中使用 dataset
  • 原文地址:https://www.cnblogs.com/xiongyu/p/2268634.html
Copyright © 2020-2023  润新知