黑马程序员——重点:自己不太懂的小题

------<a href="http://www.itheima.com"target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

自己不太懂的小题

1、请编写程序,统计一个字符串中每个字符出现的次数

答案:

public class TreeMapTest {

       publicstatic void main(String[] args) {

              //1定义字符串  "aababcabcdabcde"

              Stringstr = "aababcabcdabcde";

              //2:创建一个TreeMap集合。用来存储每个字符与它出现次数

              TreeMap<Character,Integer>map = new TreeMap<Character,Integer>();

              //3:遍历字符串,得到每一个字符

              for(int i = 0; i < str.length(); i++) {

                     charch = str.charAt(i);

                     //4:判断该字符在集合中是否存在

                     if(map.containsKey(ch)) {

                            //存在

                            //a:获取该字符对应的次数

                            intcount = map.get(ch);

                            //b:次数+1

                            count++;

                            //c:再把该字符与新的次数进行存储

                            map.put(ch,count);

                     }else {

                            //不存在:

                            //a:把该字符次数1 存储到集合中

                            map.put(ch,1);

                     }

              }

              //5:遍历集合,得到字符与对应的次数

              //a:获取所有的键值对元素对象

              Set<Entry<Character,Integer>> entrySet = map.entrySet();

              //b:获取每一个键值对元素对象

              StringBuildersb = new StringBuilder();//用来拼接字符串

              for(Entry<Character, Integer> entry : entrySet) {

                     //获取键  获取值

                     Characterch = entry.getKey();

                     Integercount = entry.getValue();

                     //6;拼接成   指定的字符串形式 a(5)b(4)c(3)d(2)e(1)

                     sb.append(ch).append("(").append(count).append(")");

              }

              //打印

              System.out.println(sb.toString());

       }

}

 

2、请编写程序,存储自定义对象到HashMap集合中,并采用两种方式遍历

答案:

class Teacher {

       privateString name;

       privateint age;

 

       publicTeacher() {

              super();

       }

       publicTeacher(String name, int age) {

              super();

              this.name= name;

              this.age= age;

       }

       publicString getName() {

              returnname;

       }

       publicvoid setName(String name) {

              this.name= name;

       }

       publicint getAge() {

              returnage;

       }

       publicvoid setAge(int age) {

              this.age= age;

       }

 

       @Override

       publicint hashCode() {

              finalint prime = 31;

              intresult = 1;

              result= prime * result + age;

              result= prime * result + ((name == null) ? 0 : name.hashCode());

              returnresult;

       }

 

       @Override

       publicboolean equals(Object obj) {

              if(this == obj)

                     returntrue;

              if(obj == null)

                     returnfalse;

              if(getClass() != obj.getClass())

                     returnfalse;

              Teacherother = (Teacher) obj;

              if(age != other.age)

                     returnfalse;

              if(name == null) {

                     if(other.name != null)

                            returnfalse;

              }else if (!name.equals(other.name))

                     returnfalse;

              returntrue;

       }

}

public class HashMapTest {

       publicstatic void main(String[] args) {

              //创建集合对象

              HashMap<Teacher,String>hm = new HashMap<Teacher,String>();

              //添加元素

              hm.put(newTeacher("小曼老师", 18),"itcast001");

              hm.put(newTeacher("范老师", 28),"itcast002" );

              hm.put(newTeacher("邹老师", 32),"itcast003");

              hm.put(newTeacher("邹老师", 32),"itcast004");

             

              //遍历

              //键找值

              Set<Teacher>keys = hm.keySet();

              for(Teacher t : keys) {

                     Stringvalue = hm.get(t);

                     System.out.println(value+"--"+ t.getName() + "--" + t.getAge());

              }

                           

              //键值对元素,找键找值

              //a: 获取所有的键值对对象

              Set<Entry<Teacher,String>> entrySet = hm.entrySet();

              //b: 获取到每一个键值对元素对象

              for(Entry<Teacher, String> entry : entrySet) {

                     //通过键值对元素对象,找键找值

                     Teachert = entry.getKey();

                     Stringvalue = entry.getValue();

                     System.out.println(  value +"--"+ t.getName() + "--"+ t.getAge());

              }

       }

}

 

3、请编写程序,存储自定义对象到TreeMap集合中,并采用两种方式遍历

答案:

class Student {

       privateString name;

       privateint age;

       publicStudent() {

              super();

       }

       publicStudent(String name, int age) {

              super();

              this.name= name;

              this.age= age;

       }

       publicString getName() {

              returnname;

       }

       publicvoid setName(String name) {

              this.name= name;

       }

       publicint getAge() {

              returnage;

       }

       publicvoid setAge(int age) {

              this.age= age;

       }

}

public class TreeMapDemo2 {

       publicstatic void main(String[] args) {

              //创建集合对象

              TreeMap<Student,String>tm = new TreeMap<Student,String>(new Comparator<Student>() {

                     @Override

                     publicint compare(Student s1, Student s2) {

                            //姓名比较排序

                            intnum = s1.getName().compareTo(s2.getName());

                            //年龄比较排序

                            intnum2 = (num==0) ? (s1.getAge() - s2.getAge()) : num;

                            returnnum2;

                     }

              });

              //添加元素

              tm.put(newStudent("z周瑜老师", 28),"itcast110");

              tm.put(newStudent("f范老师", 27),"itcast114");

              tm.put(newStudent("x小曼老师", 17),"itcast911");

              tm.put(newStudent("x小曼老师", 18),"itcast911");

              //遍历

              //键找值

              Set<Student>keys = tm.keySet();

              for(Student s : keys) {

                     Stringvalue = tm.get(s);

                     System.out.println(value + "..."+ s.getName() +"..."+s.getAge() );

              }

             

              //键值对找键找值

              Set<Entry<Student,String>> entrySet = tm.entrySet();

              for(Entry<Student, String> entry : entrySet) {

                     Students = entry.getKey();

                     Stringvalue = entry.getValue();

                     System.out.println(value + "..."+ s.getName() +"..."+s.getAge() );

              }

       }

}

 

4、请编写程序,完成集合嵌套,并遍历

传智播客

                     jc    基础班

                                   张三              20

                                   李四              22

                     jy    就业班

                                   王五              21

                                   赵六              23

答案:

       class Student {

       privateString id;

       privateString name;

       publicStudent() {

              super();

       }

       publicStudent(String id, String name) {

              super();

              this.id= id;

              this.name= name;

       }

       publicString getId() {

              returnid;

       }

       publicvoid setId(String id) {

              this.id= id;

       }

       publicString getName() {

              returnname;

       }

       publicvoid setName(String name) {

              this.name= name;

       }

}

HashMap嵌套HashMap

       public class HashMapTest {

       publicstatic void main(String[] args) {

              //1创建基础班集合 HashMap<String, String>

              HashMap<String,String> base = new HashMap<String, String>();

              //2: 向基础班集合添加元素

              base.put("01","陈如水");

              base.put("02","左帅");

             

              //3:创建就业班集合  HashMap<String, String>

              HashMap<String,String> job = new HashMap<String, String>();

              //4:向就业班集合添加元素

              job.put("01","李天成");

              job.put("02","许伟");

             

              //5创建一个czbk集合  HashMap<  String,  HashMap<String, String> >

              HashMap<String,HashMap<String, String>> czbk = new HashMap<String,HashMap<String, String>>();

              //6:czbk集合中添加班级元素

              czbk.put("基础班", base);

              czbk.put("就业班", job);

             

              //7:遍历

              //遍历学校集合,得到每一个班级

              Set<Entry<String,HashMap<String, String>>> classes = czbk.entrySet();

              //得到每一个班级

              for(Entry<String, HashMap<String, String>> clazz : classes) {

                     //获取班级名称

                     Stringname = clazz.getKey();

                     System.out.println(name);

                     //获取班级元素

                     HashMap<String,String> students = clazz.getValue();

                     //获取到每一个学生信息

                     Set<Entry<String,String>> studentSet = students.entrySet();

                     for(Entry<String, String> student : studentSet) {

                            //获取学生的学号

                            Stringid = student.getKey();

                            //获取学生的姓名

                            Stringn = student.getValue();

                            System.out.println("\t"+id+"..."+n);

                     }

              }

       }

}

HashMap嵌套ArrayList

       public classHashMapTest {

       publicstatic void main(String[] args) {

              //1创建基础班集合   ArrayList<Student>

              ArrayList<Student>base = new ArrayList<Student>();

              //2:向基础班集合添加元素

              base.add(newStudent("01","陈如水"));

              base.add(newStudent("02","左帅"));

             

              //3:创建就业班集合    ArrayList<Student>

              ArrayList<Student>job = new ArrayList<Student>();

              //4:向就业班集合添加元素

              job.add(newStudent("01","李天成"));

              job.add(newStudent("02","许伟"));

             

              //5:创建czbk学校集合    HashMap<String,  ArrayList<Student> >

              HashMap<String,ArrayList<Student>> czbk = new HashMap<String, ArrayList<Student>>();

              //6:把班级添加到学校集合中

              czbk.put("基础班", base);

              czbk.put("就业班", job);

             

              //7:遍历

              //获取到所有班级的名称

              Set<String>classNames = czbk.keySet();

              //获取到每一个班级名称

              for(String className : classNames) {

                     System.out.println(className);

                     //通过班级名称,获取到班级学生信息

                     ArrayList<Student>Students = czbk.get(className);

                     //获取到每一个学生信息

                     for(Students : Students){

                            System.out.println("\t"+s.getId()+"..."+s.getName());

                     }

              }

       }

}

ArrayList嵌套HashMap

public class ArrayListTest {

       publicstatic void main(String[] args) {

              //1创建基础班集合 HashMap<String, String>

              HashMap<String,String> base = new HashMap<String, String>();

              //2: 向基础班集合添加元素

              base.put("01","陈如水");

              base.put("02","左帅");

             

              //3:创建就业班集合  HashMap<String, String>

              HashMap<String,String> job = new HashMap<String, String>();

              //4:向就业班集合添加元素

              job.put("01","李天成");

              job.put("02","许伟");

             

              //5:创建一个czbk集合  ArrayList<HashMap<String, String>>

              ArrayList<HashMap<String,String> > czbk = new ArrayList<HashMap<String, String>>();

              //6:czbk集合中添加班级元素

              czbk.add(base);

              czbk.add(job);

              //7:遍历(难点)

              //获取到每一个班级

              for(HashMap<String, String> clazz : czbk) {

                     //获取到班级的所有学生

                     Set<Entry<String,String>> students = clazz.entrySet();

                     //获取到每一个学生

                     for(Entry<String, String> student : students) {

                            Stringid = student.getKey();

                            Stringname = student.getValue();

                            System.out.println(id+"--"+name );

                     }

              }

       }

}

5、请编写程序,完成模拟斗地主案例

答案:

public class PokerDemo {

       publicstatic void main(String[] args) {

              //创建一个HashMap集合

              HashMap<Integer,String> hm = new HashMap<Integer, String>();

 

              //创建一个ArrayList集合

              ArrayList<Integer>array = new ArrayList<Integer>();

 

              //创建花色数组和点数数组

              //定义一个花色数组

              String[]colors = { "♠", "♥", "♣", "♦" };

              //定义一个点数数组

              String[]numbers = { "3", "4", "5", "6","7", "8", "9", "10", "J","Q",

                            "K","A", "2", };

 

              //0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号即可。

              intindex = 0;

 

              for(String number : numbers) {

                     for(String color : colors) {

                            Stringpoker = color.concat(number);

                            hm.put(index,poker);

                            array.add(index);

                            index++;

                     }

              }

              hm.put(index,"小王");

              array.add(index);

              index++;

              hm.put(index,"大王");

              array.add(index);

 

              //洗牌(洗的是编号)

              Collections.shuffle(array);

 

              //发牌(发的也是编号,为了保证编号是排序的,就创建TreeSet集合接收)

              TreeSet<Integer>sunQuan = new TreeSet<Integer>();

              TreeSet<Integer>caoCao = new TreeSet<Integer>();

              TreeSet<Integer>liuBei = new TreeSet<Integer>();

              TreeSet<Integer>diPai = new TreeSet<Integer>();

 

              for(int x = 0; x < array.size(); x++) {

                     if(x >= array.size() - 3) {

                            diPai.add(array.get(x));

                     }else if (x % 3 == 0) {

                            sunQuan.add(array.get(x));

                     }else if (x % 3 == 1) {

                            caoCao.add(array.get(x));

                     }else if (x % 3 == 2) {

                            liuBei.add(array.get(x));

                     }

              }

 

              //看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)

              lookPoker("孙权", sunQuan, hm);

              lookPoker("曹操", caoCao, hm);

              lookPoker("刘备", liuBei, hm);

              lookPoker("底牌", diPai, hm);

       }

 

       //写看牌的功能

       publicstatic void lookPoker(String name, TreeSet<Integer> ts,

                     HashMap<Integer,String> hm) {

              System.out.print(name+ "的牌是:");

              for(Integer key : ts) {

                     Stringvalue = hm.get(key);

                     System.out.print(value+ " ");

              }

              System.out.println();

       }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值