DAY09Map接口、斗地主案例(有序版本)、冒泡排序
- 游戏开发
- 2025-09-22 01:27:01

学习目标 能够说出Map集合特点 1.Map集合是一个双列集合,每个元素包含两个值,一个key一个value 2.Map集合中的key是不允许重复的,value可以重复 3.map集合中一个key只能对应一个value值 4.Map集合中key和value数据类型是可以相同的,也可以不同 使用Map集合添加方法保存数据 V put(K key, V value) 把指定的键与值添加到Map集合中 返回值:V(值) key不重复,返回值就是null key重复,会使用新的value,替换之前的value,返回的就是被替换的value值 Map<String,String> map = new HashMap<>(); map.put("黄晓明", "杨颖"); //v=null map.put("黄晓明", "赵薇"); //v=杨颖 map.put("冷锋","龙小云"); map.put("杨过","小龙女"); 使用”键找值”的方式遍历Map集合 1.使用Map集合中的方法keySet,把所有的key取出来,存储到一个Set集合中 2.遍历Set集合,获取到Map集合中的每一个key 3.使用Map集合中的方法get,根据key找到value Set<String> set = map.keySet(); for (String key : set) { Integer value = map.get(key); System.out.println(key+"\t"+value); } 使用”键值对”的方式遍历Map集合 1.使用Map集合中的方法entrySet,获取Map集合中的所有Entry对象,存储到一个Set集合中 2.遍历Set集合,获取每一个Entry对象 3.使用Entry对象中的方法getKey和getValue获取键与值 Set<Map.Entry<String, String>> set = map.entrySet(); for (Map.Entry<String, String> entry : set) { String key = entry.getKey(); String value = entry.getValue(); System.out.println(key+"\t"+value); } 能够使用HashMap存储自定义键值对的数据 自定义类型(Person,Student)作为key:保证同名同年龄的人视为同一个人,需要重写hashCode和equals方法 HashMap<String,Person> 不需要重写hashCode和equals方法,值是可以重复的 HashMap<Person,String> 需要重写hashCode和equals方法,key是不允许重复的 能够完成斗地主洗牌发牌案例 1.准备牌 2.洗牌 3.发牌 4.排序 5.看牌 能够完成冒泡排序 使用数组中相邻元素,依次比较,每次选出最大的元素 第一章 Map集合 1.Map集合的概述 java.util.Map<k,v>:接口 key:键 value:值 特点: 1.Map集合是一个双列集合,每个元素包含两个值,一个key一个value 2.Map集合中的key是不允许重复的,value可以重复 3.Map集合中一个key只能对应一个value 4.Map集合中key和value数据类型可以是相同的,也可以是不同 2.Map集合常用的子类 1.java.util.HashMap<K,V>集合 implements Map<K,V>接口 基于哈希表的 Map 接口的实现。此类不保证映射的顺序,特别是它不保证该顺序恒久不变。 a.HashMap集合底层是一个哈希表结构和HashSet是一样 b.是一个无序集合 2.java.util.LinkedHashMap<K,V>集合 extends HashMap<K,V>集合 Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序。此实现与 HashMap 的不同之处在于,后者维护着一个运行于所有条目的双重链接列表。 a.LinkedHashMap集合底层是哈希表+单向链表和LinkedHashSet是一样 b.是一个有序的集合 3.java.util.TreeMap<K,V>集合 implements Map<K,V>接口 基于红黑树(Red-Black tree)的 NavigableMap 实现。该映射根据其键的自然顺序进行排序,或者根据创建映射时提供的 Comparator 进行排序,具体取决于使用的构造方法。 a.TreeMap底层是一个红黑树结构 b.TreeMap集合自带了一个比较器,里边存储的key是有序的:默认是升序或者可以根据比较器自定义排序规则 和TreeSet是一样 3.Map的常用方法(重点) package com.itheima.demo01Map; import java.util.Collection; import java.util.HashMap; import java.util.Map; /* Map接口中的常用方法 V put(K key, V value) 把指定的键与值添加到Map集合中 V remove(Object key) 根据指定的键,删除键值对,返回被删除的值 V get(Object key) 根据key获取value值 boolean containsKey(Object key) 判断集合中是否包含指定的key */ public class Demo01Map { public static void main(String[] args) { show05(); } /* Collection<V> values() 获取Map集合中所有的value值,把value值存储到一个Collection集合中 */ private static void show05() { Map<String,Integer> map = new HashMap<>(); map.put("迪丽热巴",168); map.put("古力娜扎",165); map.put("冯提莫",150); map.put("林志玲",178); Collection<Integer> coll = map.values(); System.out.println(coll);//[178, 168, 150, 165] } /* boolean containsKey(Object key) 判断集合中是否包含指定的key boolean containsValue(Object value) 判断集合中是否包含指定的value */ private static void show04() { Map<String,Integer> map = new HashMap<>(); map.put("迪丽热巴",168); map.put("古力娜扎",165); map.put("冯提莫",150); map.put("林志玲",178); boolean b1 = map.containsKey("冯提莫"); System.out.println("b1:"+b1);//b1:true boolean b2 = map.containsKey("赵丽颖"); System.out.println("b2:"+b2);//b2:false System.out.println(map.containsValue(150));//true System.out.println(map.containsValue(188));//false } /* V get(Object key) 根据key获取value值 返回值:V(值) key存在:返回对应的value值 key不存在:返回null */ private static void show03() { Map<String,Integer> map = new HashMap<>(); map.put("迪丽热巴",168); map.put("古力娜扎",165); map.put("冯提莫",150); map.put("林志玲",178); Integer v1 = map.get("迪丽热巴"); System.out.println("v1:"+v1);//v1:168 //int v2 = map.get("林志颖");//NullPointerException 自动拆箱 Integer v2 = map.get("林志颖"); System.out.println("v2:"+v2);//v2:null } /* V remove(Object key) 根据指定的键,删除键值对,返回被删除的值 返回值:V(值) key存在:删除对应的键值对,返回被删除键值对中的值 key不存在:对集合没有影响,返回null */ private static void show02() { Map<String,Integer> map = new HashMap<>(); map.put("迪丽热巴",168); map.put("古力娜扎",165); map.put("冯提莫",150); map.put("林志玲",178); System.out.println(map);//{林志玲=178, 迪丽热巴=168, 冯提莫=150, 古力娜扎=165} Integer v1 = map.remove("林志玲"); System.out.println("v1:"+v1);//v1:178 System.out.println(map);//{ 迪丽热巴=168, 冯提莫=150, 古力娜扎=165} Integer v2 = map.remove("马尔扎哈"); System.out.println("v2:"+v2);//v2:null System.out.println(map);//{ 迪丽热巴=168, 冯提莫=150, 古力娜扎=165} } /* V put(K key, V value) 把指定的键与值添加到Map集合中 返回值:V(值) 添加元素的时候,key不重复,返回值V就是null 添加元素的时候,key重复,会使用新的value,替换之前的value,返回的值V就是被替换的value */ private static void show01() { //创建Map集合对象,泛型使用<String,String> Map<String,String> map = new HashMap<>(); String v1 = map.put("黄晓明", "杨颖"); System.out.println("v1:"+v1);//v1:null System.out.println(map);//{黄晓明=杨颖} String v2 = map.put("黄晓明", "赵薇"); System.out.println("v2:"+v2);//v2:杨颖 System.out.println(map);//{黄晓明=赵薇} map.put("冷锋","龙小云"); map.put("杨过","小龙女"); map.put("郭靖","黄蓉"); map.put("尹志平","小龙女"); System.out.println(map);//{杨过=小龙女, 尹志平=小龙女, 郭靖=黄蓉, 冷锋=龙小云, 黄晓明=赵薇} } } 4.Map集合的遍历(重点) 1).键找值的方式 概述 代码实现 package com.itheima.demo01Map; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /* Map集合的遍历_键找值方式 使用到的方法: Set<K> keySet() 把所有的key取出来,存储到一个Set集合中 V get(Object key) 根据key获取value值 实现步骤: 1.使用Map集合中的方法keySet,把所有的key取出来,存储到一个Set集合中 2.遍历Set集合,获取Map集合中的每一个key 3.使用Map集合中的方法get,根据key获取value值 */ public class Demo02Map { public static void main(String[] args) { Map<String,Integer> map = new HashMap<>(); map.put("迪丽热巴",168); map.put("古力娜扎",165); map.put("冯提莫",150); map.put("林志玲",178); //1.使用Map集合中的方法keySet,把所有的key取出来,存储到一个Set集合中 Set<String> set = map.keySet(); //2.遍历Set集合,获取Map集合中的每一个key //使用迭代器遍历Set集合 Iterator<String> it = set.iterator(); while (it.hasNext()){ String key = it.next(); //3.使用Map集合中的方法get,根据key获取value值 Integer value = map.get(key); System.out.println(key+"="+value); } System.out.println("-------------------------"); //使用增强for循环遍历Set集合 for (String key : set) { //3.使用Map集合中的方法get,根据key获取value值 Integer value = map.get(key); System.out.println(key+"="+value); } } } 2).键值对方式 概述 代码实现 package com.itheima.demo01Map; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /* Map集合的遍历_键值对方式 使用Map集合中的方法: Set<Map.Entry<K,V>> entrySet() 返回此映射中包含的映射关系的 Set 视图。 Entry接口中的方法: K getKey() 返回与此项对应的键。 V getValue() 返回与此项对应的值。 实现步骤: 1.使用Map集合中的方法entrySet,获取Map集合中所有的Entry对象,存储到一个Set集合中 2.遍历Set集合,获取每一个Entry对象 3.使用Entry对象中的方法getKey和getValue获取键与值 */ public class Demo03Map { public static void main(String[] args) { Map<String,String> map = new HashMap<>(); map.put("郭靖","黄蓉"); map.put("杨过","小龙女"); map.put("张无忌","赵敏"); //1.使用Map集合中的方法entrySet,获取Map集合中所有的Entry对象,存储到一个Set集合中 //成员内部类:外部类名.内部类名 Map.Entry Set<Map.Entry<String, String>> entrySet = map.entrySet(); //2.遍历Set集合,获取每一个Entry对象 //使用迭代器遍历Set集合 Iterator<Map.Entry<String, String>> it = entrySet.iterator(); while (it.hasNext()){ Map.Entry<String, String> entry = it.next(); //3.使用Entry对象中的方法getKey和getValue获取键与值 String key = entry.getKey(); String value = entry.getValue(); System.out.println(key+"="+value); } System.out.println("----------------------------"); //使用增强for循环遍历Set集合 集合名.for for (Map.Entry<String, String> entry : entrySet) { //3.使用Entry对象中的方法getKey和getValue获取键与值 String key = entry.getKey(); String value = entry.getValue(); System.out.println(key+"="+value); } } } 5.HashMap存储自定义类型(重点) package com.itheima.demo01Map; import java.util.HashMap; import java.util.Map; import java.util.Set; /* HashMap存储自定义类型(重点) */ public class Demo04HashMapSaveStudent { public static void main(String[] args) { show02(); } /* HashMap存储自定义类型(重点) key:Student类型 保证同名同年龄的学生,视为同一个学生,只能存储一次 作为key的元素Student必须重写hashCode和equals方法,保证key唯一 value:String类型 可以重复 */ private static void show02() { //创建HashMap集合对象 HashMap<Student,String> map = new HashMap<>(); //往集合中添加键值对 map.put(new Student("张三",18),"中国"); map.put(new Student("女王",18),"英国"); map.put(new Student("李四",2),"朝鲜"); map.put(new Student("王五",38),"俄罗斯"); map.put(new Student("女王",18),"毛里求斯"); //使用entrySet方法+增强for循环遍历Map集合 //1.使用Map集合中的方法entrySet,获取Map集合中所有entry对象,存储到一个Set集合中 Set<Map.Entry<Student, String>> set = map.entrySet(); //2.遍历Set集合,获取每一个Entry对象 for (Map.Entry<Student, String> entry : set) { //3.使用Entry对象中的方法getkey和getValue,分别获取键与值 Student key = entry.getKey(); String value = entry.getValue(); System.out.println(key+"-->"+value); } System.out.println(map.size());//4 } /* HashMap存储自定义类型(重点) key: String类型 key是唯一,不重复:String类重写了hashCode和equals方法,所以可以保证唯一 value:Student类型 value是可以重复的 */ private static void show01() { //创建HashMap集合对象 HashMap<String,Student> map = new HashMap<>(); //往集合中存储键值对 map.put("中国",new Student("张三",18)); map.put("美国",new Student("李四",75)); map.put("俄罗斯",new Student("王五",30)); map.put("朝鲜",new Student("三胖",1)); map.put("美国",new Student("公主",18)); //使用keySet方法+增强for循环遍历Map集合 //1.使用Map集合中的方法keySet,取出所有的key,存储到一个Set集合中 Set<String> set = map.keySet(); //2.遍历Set集合,获取每一个key for (String key : set) { //3.根据key获取value Student value = map.get(key); System.out.println(key+"--->"+value); } } } package com.itheima.demo01Map; /* 思考:自定义类型,什么时候重写hashCode和equals方法 使用HashSet集合存储自定义类型 使用HashMap集合,key使用自定义类型的时候(key不重复) */ public class Student { private String name; private int age; public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; if (age != student.age) return false; return name != null ? name.equals(student.name) : student.name == null; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + age; return result; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 6.LinkedHashMap集合(了解) package com.itheima.demo01Map; import java.util.HashMap; import java.util.LinkedHashMap; /* java.util.LinkedHashMap<K,V>集合 extends HashMap<K,V>集合 Map 接口的哈希表和链接列表实现,具有可预知的迭代顺序。 此实现与 HashMap 的不同之处在于,后者维护着一个运行于所有条目的双重链接列表。 LinkedHashMap特点: a.LinkedHashMap集合底层是哈希表+单向链表和LinkedHashSet是一样 b.是一个有序的集合 */ public class Demo05LinkedHashMap { public static void main(String[] args) { HashMap<String,String> map = new HashMap<>(); map.put("aaa","111"); map.put("bbb","222"); map.put("ccc","333"); map.put("aaa","444"); map.put("ddd","555"); System.out.println(map);//{aaa=444, ccc=333, bbb=222, ddd=555} key不允许重复,是一个无序的集合 LinkedHashMap<String,String> linked = new LinkedHashMap<>(); linked.put("aaa","111"); linked.put("bbb","222"); linked.put("ccc","333"); linked.put("aaa","444"); linked.put("ddd","555"); System.out.println(linked);//{aaa=444, bbb=222, ccc=333, ddd=555} key不允许重复,是一个有序的集合 } } 7.TreeMap集合(使用) package com.itheima.demo01Map; import java.util.Comparator; import java.util.TreeMap; /* java.util.TreeMap<K,V>集合 implements Map<K,V>接口 基于红黑树(Red-Black tree)的 NavigableMap 实现。该映射根据其键的自然顺序进行排序,或者根据创建映射时提供的 Comparator 进行排序,具体取决于使用的构造方法。 TreeMap集合的特点: a.TreeMap底层是一个红黑树结构 b.TreeMap集合自带了一个比较器,里边存储的key是有序的:默认是升序或者可以根据比较器自定义排序规则 和TreeSet是一样 构造方法: TreeMap() 使用键的自然顺序构造一个新的、空的树映射。 TreeMap(Comparator<? super K> comparator) 构造一个新的、空的树映射,该映射根据给定比较器进行排序。 */ public class Demo06TreeMap { public static void main(String[] args) { TreeMap<Integer,String> map = new TreeMap<>(); map.put(1,"a"); map.put(3,"b"); map.put(2,"c"); map.put(5,"d"); map.put(4,"e"); map.put(1,"w"); System.out.println(map);//{1=w, 2=c, 3=b, 4=e, 5=d} 根据key默认升序排序 TreeMap<Integer,String> map2 = new TreeMap<>(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { //根据key降序排序 o2-o1 升序:o1-o2 return o2-o1; } }); map2.put(1,"a"); map2.put(3,"b"); map2.put(2,"c"); map2.put(5,"d"); map2.put(4,"e"); map2.put(1,"w"); System.out.println(map2);//{5=d, 4=e, 3=b, 2=c, 1=w} key按照比较器规则排序 } } 8.Hashtable集合(了解–>面试) package com.itheima.demo01Map; import java.util.HashMap; import java.util.Hashtable; /* Hashtable集合(了解-->面试) java.util.Hashtable<K,V>集合 implements Map<K,V>接口 此类实现一个哈希表,该哈希表将键映射到相应的值。任何非 null 对象都可以用作键或值。 -------------------------------------------------- HashMap集合的特点: 1.HashMap集合底层是一个哈希表 2.HashMap集合是jdk1.2版本之后出现的 3.HashMap集合运行存储null值和null键 4.HashMap集合是不同步的,效率高(多线程不安全) Hashtable集合的特点: 1.Hashtable集合底层是一个哈希表 2.Hashtable集合是jdk1.0时期出现的双列集合(最早期) 3.Hashtable集合不允许存储null值和null键 4.Hashtable集合是同步的,效率低(多线程安全) ----------------------------------------------------- Hashtable集合效率没有HashMap高,所以已经被淘汰了 但是Hashtable集合的子类Properties集合依然活跃在历史的舞台 */ public class Demo07Hashtable { public static void main(String[] args) { HashMap<String,String> map = new HashMap<>(); map.put("a",null); map.put(null,"b"); map.put(null,null); System.out.println(map);//有几对元素 {null=null, a=null} Hashtable<String,String> table = new Hashtable<>(); //table.put("a",null);//NullPointerException //table.put(null,"b");//NullPointerException table.put(null,null);//NullPointerException } } 9.Map集合的练习 需求:
输入一个字符串,统计字符串中每个字符出现次数
分析: 代码实现: package com.itheima.demo01Map; import java.util.HashMap; import java.util.Scanner; /* 需求: 输入一个字符串,统计字符串中每个字符出现次数 分析: 1.使用Scanner获取用户输入的一个字符串 2.创建一个Map集合,key使用Character类型存储字符,value值使用Integer类型,存储字符个数 3.遍历字符串,获取字符串中每一个字符 a.使用length()方法+charAt(i)方法遍历字符串 b.使用toCharArray()方法,把字符串转换为字符数组,遍历数组 4.使用Map集合中的方法containsKey,判断集合中是否包含指定的key(遍历得到的字符) boolean containsKey(Object key) true:包含指定的字符 a.根据key(遍历得到的字符),获取value(字符个数) b.value++ c.把改变后的value存储到Map集合中(更新) false:不包含指定的字符,字符第一次往集合中存储 put("字符",1); */ public class Demo08Test { public static void main(String[] args) { //1.使用Scanner获取用户输入的一个字符串 System.out.println("请输入一个字符串:"); String s = new Scanner(System.in).nextLine(); //2.创建一个Map集合,key使用Character类型存储字符,value值使用Integer类型,存储字符个数 HashMap<Character,Integer> map = new HashMap<>(); //3.遍历字符串,获取字符串中每一个字符 //a.使用length()方法+charAt(i)方法遍历字符串 /*for(int i=0; i<s.length(); i++){ char c = s.charAt(i); System.out.println(c); }*/ //b.使用toCharArray()方法,把字符串转换为字符数组,遍历数组 char[] arr = s.toCharArray(); for (char c : arr) { //4.使用Map集合中的方法containsKey,判断集合中是否包含指定的key(遍历得到的字符) //boolean containsKey(Object key) if(map.containsKey(c)){ //true:包含指定的字符 //a.根据key(遍历得到的字符),获取value(字符个数) Integer value = map.get(c); //b.value++ value++; //c.把改变后的value存储到Map集合中(更新) map.put(c,value); }else{ //false:不包含指定的字符,字符第一次往集合中存储 //put("字符",1); map.put(c,1); } /* JDK1.8版本之后Map集合新增了一个方法(了解) default V getOrDefault(Object key, V defaultValue) 返回指定键映射到的值,如果此映射不包含该键的映射,则返回 defaultValue 。 参数: Object key:map集合中的key V defaultValue:给对应key设置一个默认的value值 返回值: V:如果集合中key存在,根据key获取value值返回 如果集合中key不存在,返回设置的默认值 */ //Integer value = map.getOrDefault(c, 0); //map.put(c,++value); } System.out.println(map);//{a=4, b=4, c=2} } } 10.集合嵌套 集合的嵌套:集合中的元素仍然是一个集合任何集合之间都可以相互嵌套List集合可以嵌套Set集合,Set集合也可以嵌套List集合,List集合可以嵌套Map集合…注意:只要看到集合就遍历 1).List嵌套List(重点) package com.itheima.demo02Collection; import java.util.ArrayList; /* List嵌套List(重点) */ public class Demo01ListList { public static void main(String[] args) { //假如我们有两个班级的学生,分别存储到两个List集合中 //定义一个存储第一个班级学生的List集合 ArrayList<String> list01 = new ArrayList<>(); list01.add("迪丽热巴"); list01.add("古力娜扎"); list01.add("赵丽颖"); list01.add("杨幂"); //定义一个存储第二个班级学生的List集合 ArrayList<String> list02 = new ArrayList<>(); list02.add("胡歌"); list02.add("蔡徐坤"); list02.add("霍建华"); list02.add("陈伟霆"); //定义一个List集合存储两个班级 ArrayList<ArrayList<String>> allList = new ArrayList<>(); //把存储学生的List集合添加到集合中 allList.add(list01); allList.add(list02); //遍历allList集合,后驱集合中存储的每一个元素(ArrayList集合) for (ArrayList<String> list : allList) { //遍历每个班级的List集合,获取每一个学生的姓名 for (String name : list) { System.out.println(name); } } } } 2).List集合嵌套Map集合 package com.itheima.demo02Collection; import java.util.ArrayList; import java.util.HashMap; import java.util.Set; /* List集合嵌套Map集合 */ public class Demo02ListMap { public static void main(String[] args) { /* 两个班级的学生,分别存储在两个Map集合中 key:学生的学号 value:学生的姓名 */ //定义第一个班级的Map集合 HashMap<String,String> map1 = new HashMap<>(); map1.put("it001","迪丽热巴"); map1.put("it002","古力娜扎"); //定义第二个班级的Map集合 HashMap<String,String> map2 = new HashMap<>(); map2.put("hm001","胡歌"); map2.put("hm002","霍建华"); //定义一个ArrayList集合,泛型使用HashMap<String,String>,存储两个班级的Map集合 ArrayList<HashMap<String,String>> list = new ArrayList<>(); list.add(map1); list.add(map2); //遍历List集合,获取List集合中存储的每一个Map集合 for (HashMap<String, String> map : list) { //遍历Map集合,获取Map集合中的key和value Set<String> set = map.keySet(); for (String key : set) { String value = map.get(key); System.out.println(key+"\t"+value); } System.out.println("-------------------------"); } } } 3).Map集合嵌套Map集合 package com.itheima.demo02Collection; import java.util.HashMap; import java.util.Set; /* Map集合嵌套Map集合 */ public class Demo03MapMap { public static void main(String[] args) { /* 两个班级的学生,分别存储在两个Map集合中 key:学生的学号 value:学生的姓名 */ //定义第一个班级的Map集合 HashMap<String,String> map1 = new HashMap<>(); map1.put("it001","迪丽热巴"); map1.put("it002","古力娜扎"); //定义第二个班级的Map集合 HashMap<String,String> map2 = new HashMap<>(); map2.put("hm001","胡歌"); map2.put("hm002","霍建华"); /* 创建Map集合 key:存储班级的名称 传智888期 黑马888期 value:存储学生的Map集合 map1 map2 */ HashMap<String,HashMap<String,String>> mapmap = new HashMap<>(); //往集合中添加元素 mapmap.put("传智888期",map1); mapmap.put("黑马888期",map2); //使用keySet方法获取mapmap集合中所有的key(班级名称),把key存储到一个Set集合中 Set<String> classNameSet = mapmap.keySet(); //遍历存储班级名称的Set集合,获取每一个班级名称(key) for (String className : classNameSet) { //根据班级名称(key),获取到集合中存储的value(存储班级的Map集合) HashMap<String, String> classMap = mapmap.get(className); //使用keySet方法获取classMap集合中所有的key(学号),把key存储到一个Set集合中 Set<String> stuNumberSet = classMap.keySet(); //遍历Set集合获取每一个学生的学号(key) for (String stuNumber : stuNumberSet) { //根据学生的学号(key)获取学生的姓名(value) String name = classMap.get(stuNumber); System.out.println("班级的名称:"+className+",学号:"+stuNumber+",姓名:"+name); } } } } 第二章 斗地主综合案例:有序版本(重点) 1.需求分析: 2.代码实现: package com.itheima.demo03Test; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; /* 斗地主综合案例: 1.准备牌 2.洗牌 3.发牌 4.排序 5.看牌 */ public class DouDiZhu { public static void main(String[] args) { //1.准备牌 //定义HashMap集合,key:integer存储牌的索引,value:String存储组装好的牌 HashMap<Integer,String> poker = new HashMap<>(); //定义ArrayList集合,泛型Integer,存储牌的索引 ArrayList<Integer> pokerIndex = new ArrayList<>(); //定义一个变量,记录牌的索引(0-53) int index= 0; //定义两个ArrayList集合,一个存储牌的花色,一个存储牌的序号 ArrayList<String> numbers = new ArrayList<>(); ArrayList<String> colors = new ArrayList<>(); //往集合中存储序号和花色 Collections.addAll(numbers,"2","A","K","Q","J","10","9","8","7","6","5","4","3"); Collections.addAll(colors,"♠","♥","♣","♦"); //往扑克集合中存储大王和小王 poker.put(index,"大王"); pokerIndex.add(index); index++;//改变牌的索引0->1 poker.put(index,"小王"); pokerIndex.add(index); index++;//改变牌的索引1->2 //嵌套遍历两个List集合,组装52张牌(numbers集合必须写在外层) for (String number : numbers) { for (String color : colors) { //System.out.println(color+number); //把组装好的牌存储到集合中 poker.put(index,color+number); pokerIndex.add(index); index++;//改变牌的索引2->3-->4-->5....53 } } //System.out.println(poker); //System.out.println(pokerIndex); //2.洗牌 使用Collections集合中的shuffle(list)方法 随机打乱集合中元素的顺序 Collections.shuffle(pokerIndex); //System.out.println(pokerIndex); //3.发牌 //定义4个ArrayList集合存储玩家的牌和底牌 ArrayList<Integer> player01 = new ArrayList<>(); ArrayList<Integer> player02 = new ArrayList<>(); ArrayList<Integer> player03 = new ArrayList<>(); ArrayList<Integer> diPai = new ArrayList<>(); //遍历存储牌的索引的ArrayList集合(pokerIndex),获取每一个牌的索引 for (int i = 0; i < pokerIndex.size(); i++) { Integer pi = pokerIndex.get(i); //使用集合的索引%3给3个玩家发牌,索引>=51给底牌发牌(注意:必须先判断底牌) if(i>=51){ //给底牌发牌 diPai.add(pi); }else if(i%3==0){ //给玩家1发牌 player01.add(pi); }else if(i%3==1){ //给玩家2发牌 player02.add(pi); }else if(i%3==2){ //给玩家3发牌 player03.add(pi); } } //System.out.println(player01); //System.out.println(player02); //System.out.println(player03); //System.out.println(diPai); //4.排序 使用Collections集合工具类中的方法sort(list) 对List集合进行升序排序 Collections.sort(player01); Collections.sort(player02); Collections.sort(player03); Collections.sort(diPai); //System.out.println(player01); //System.out.println(player02); //System.out.println(player03); //System.out.println(diPai); //5.看牌 调用看牌的方法 lookPoker("刘德华",player01,poker); lookPoker("周润发",player02,poker); lookPoker("周星驰",player03,poker); lookPoker("底牌",diPai,poker); } /* 定义一个看牌的方法:目的提高代码的复用性 参数: String name:玩家名称 ArrayList<Integer> list:玩家和底牌集合 HashMap<Integer,String> poker:存储牌的索引和组装好的牌的poker集合 实现方法:查表法 遍历玩家|底牌集合,获取Map集合的每一个key,根据key找到value */ public static void lookPoker(String name,ArrayList<Integer> list,HashMap<Integer,String> poker){ //打印玩家名称,不换行 System.out.print(name+": "); //遍历玩家|底牌集合,获取Map集合的每一个key for (Integer key : list) { //根据key找到value String value = poker.get(key); //打印查找到的牌,不换行 System.out.print(value+" "); } //打印完每一个玩家|底牌的所有牌,换行 System.out.println(); } } 第三章 冒泡排序(重点) 1.原理 2.代码实现 package com.itheima.demo04bubbleSort; import java.util.Arrays; /* 冒泡排序 使用数组中相邻的元素依次比较,每次选出最大的元素 */ public class Demo01BubbleSort { public static void main(String[] args) { int[] arr = {5,1,8,3}; System.out.println("排序前数组中的元素:"+ Arrays.toString(arr));//排序前数组中的元素:[5, 1, 8, 3] /* 冒泡排序 1.定一个循环嵌套 外层循环:控制比较的次数:arr.length-1 内层循环:控制每次比较几对元素 为了防止索引越界异常: arr.lenth-1 为了让每次比较的元素对数依次减少: arr.length-1-i 2.在循环中依次比较arr[j]和arr[j+1]的值,选出最大的 比较两个元素,使用第三方变量交换位置 */ //1.定一个循环嵌套 for (int i = 0; i < arr.length - 1; i++) {//外层循环:控制比较的次数:arr.length-1 for (int j = 0; j < arr.length - 1 - i; j++) {//内层循环:控制每次比较几对元素 //2.在循环中依次比较arr[j]和arr[j+1]的值,选出最大的 if(arr[j]>arr[j+1]){ //比较两个元素,使用第三方变量交换位置 int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } System.out.println("排序后数组中的元素:"+Arrays.toString(arr));//排序后数组中的元素:[1, 3, 5, 8] } }DAY09Map接口、斗地主案例(有序版本)、冒泡排序由讯客互联游戏开发栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“DAY09Map接口、斗地主案例(有序版本)、冒泡排序”