在我们的日常工作与生活当中,java都有涉及到。它具有十分强大的功能,并且内容也极其丰富,使用频率也是很高的,所以大家对于java的学习热情也是十分积极的。今天就来为大家介绍java字典,多层字典,斗地主发牌的实例展示,一起来看看吧。
首先是一个简单的介绍,如下所示:
字典 Map 存键值对
/ \
hashmap
自定义键对象(不能重)需要重写键的hashCode()方法、equals()方法。
map实例代码如下:
import java.util.Map; import java.util.Set; public class Demo01 { public static void main(String[] args) { Map < String, String > map = new HashMap < String, String > (); map.put("a", "12"); //添加 map.put("b", "1"); map.put("a", "2"); //覆盖a的值 System.out.println(map); //{a=2,b=1} 无顺序 //String v=map.remove("b");//删除键的值 返回 //System.out.println(v); System.out.println(map.get("a")); //取值 没有返回null // 通过keyset遍历map 增强for // Map不能直接用 迭代器/增强for 遍历 需要转Set才能用 Set < String > keys = map.keySet(); // 返回集合中所有键 for (String key: keys) { String value = map.get(key); // 获取键的值 System.out.println(key + "..." + value); } //通过Iterater Set < String > keys2 = map.keySet(); Iterator < String > it = keys2.iterator(); while (it.hasNext()) { String key = it.next(); String value = map.get(key); System.out.println(key + "..." + value)); } //增强for + entrySet方法(键值对对象) Set < Map.Entry < String, String >> entrys = map.entrySet(); for (Map.Entry < String, String > entry: entrys) { String key = entry.getKey(); String value = entry.getValue(); System.out.println(key + "..." + value); } //Iterater遍历 Set < Map.Entry < String, String >> entrys2 = map.entrySet(); Iterator < Map.Entry < String, String >> it2 = entrys.iterator(); while (it2.hasNext()) { Map.Entry < String, String > entry2 = it2.next(); String key2 = entry2.getKey(); String value2 = entry2.getValue(); System.out.println(key2 + "..." + value2); } } } Map < Student, String > map = new HashMap < Student, String > (); //键内容不重 map.put(new Student("lisi", 28), "上海"); // Student重写HashCode()、equals()
多层字典实例代码如下:
HashMap < String, HashMap < String, Person >> oracle = new HashMap < String, HashMap < String, Person >> (); HashMap < String, Person > java0601 = new HashMap < String, Person > (); java0601.put("001", new Person("小红帽", 18)); java0601.put("002", new Person("小丸子", 20)); HashMap < String, Person > java0929 = new HashMap < String, Person > (); java0929.put("001", new Person("张超", 22)); java0929.put("002", new Person("赵玉仙", 21)); oracle.put("java0601", java0601); oracle.put("java0929", java0929); //遍历 增强for+keyset Set < String > classes = oracle.keySet(); //所有键 集合 班级名字集合 for (String clas: classes) { HashMap < String, Person > c = oracle.get(clas); //获取班级集合 值 Set < String > stunumbers = c.keySet(); // 人名集合 键 for (String num: stunumbers) { Person p = c.get(num); //人名集合 值 System.out.println("班级" + clas + "学号" + num + "学生" + p); } }
斗地主发牌实例代码如下:
public static void main(String[] args) { //准备容器 //牌序 牌值 HashMap < Integer, String > pookerMap = new HashMap < Integer, String > (); ArrayList < Integer > pookerNumer = new ArrayList < Integer > (); //牌序 //准备牌 String[] number = { "2" , "A" , "K" , "Q" , "J" , "10" , "9" , "8" , "7" , "6" , "5" , "4" , "3" }; String[] color = { "♥" , "♦" , "♣" , "♠" }; int index = 2; for (String num: number) { for (String col: color) { pookerMap.put(index, col + num); //向Map中添加 牌序:牌值 pookerNumer.add(index); //向ArrayList添加 牌序号 index++; } } //添加大小王 pookerMap.put(0, "大王"); pookerNumer.add(0); pookerMap.put(1, "小王"); pookerNumer.add(1); System.out.println(pookerMap); //洗牌、发牌 Collections.shuffle(pookerNumer); //打乱 ArrayList 牌序号 ArrayList < Integer > player1 = new ArrayList < Integer > (); ArrayList < Integer > player2 = new ArrayList < Integer > (); ArrayList < Integer > player3 = new ArrayList < Integer > (); ArrayList < Integer > bottom = new ArrayList < Integer > (); for (int i = 0; i < pookerNumer.size(); i++) { //分发 牌序号 if (i < 3) { bottom.add(pookerNumer.get(i)); } // 前三个序号 添加 底牌 else if (i % 3 == 0) { player1.add(pookerNumer.get(i)); } else if (i % 3 == 1) { player2.add(pookerNumer.get(i)); } else if (i % 3 == 2) { player3.add(pookerNumer.get(i)); } } //对三个玩家的牌 排序 Collections.sort(player1); Collections.sort(player2); Collections.sort(player3); Collections.sort(bottom); //看牌 lookPooker("玩家1", player1, pookerMap); // 名称 牌序号 字典 lookPooker("玩家2", player2, pookerMap); lookPooker("玩家3", player3, pookerMap); lookPooker("底牌", bottom, pookerMap); } public static void lookPooker(String name, ArrayList < Integer > player, HashMap < Integer, String > pookerMap) { System.out.println(name + ": "); for (int index: player) { System.out.print(pookerMap.get(index) + " "); // 对应字典 找出 牌序号的 值 } System.out.println(); }
以上就是关于java字典,多层字典,斗地主发牌的主要实例展示了。如果你对java知识感兴趣,想要了解更多java经典例子和常见问题,敬请关注奇Q工具网。
推荐阅读: