麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

黑馬程序員_JavaSE學(xué)習(xí)總結(jié)第18天_集合框架4

2019-11-15 00:18:53
字體:
供稿:網(wǎng)友
黑馬程序員_javaSE學(xué)習(xí)總結(jié)第18天_集合框架4

------- android培訓(xùn)、java培訓(xùn)、期待與您交流! ----------

18.01 Map集合概述和特點(diǎn)

Map接口概述:將鍵映射到值的對象,一個(gè)映射不能包含重復(fù)的鍵,每個(gè)鍵最多只能映射到一個(gè)值

Map接口和Collection接口的不同

1.Map是雙列的,Collection是單列的

2.Map的鍵唯一,Collection的子體系Set是唯一的

3.Map集合的數(shù)據(jù)結(jié)構(gòu)值針對鍵有效,跟值無關(guān),Collection集合的數(shù)據(jù)結(jié)構(gòu)是針對元素有效

18.02 Map集合的功能概述

成員方法:

1.V put(K key,V value):

將指定的值與此映射中的指定鍵關(guān)聯(lián)(可選操作)。

2.V remove(Object key):

如果存在一個(gè)鍵的映射關(guān)系,則將其從此映射中移除(可選操作)。

3.void clear():

從此映射中移除所有映射關(guān)系(可選操作)。

4.boolean containsKey(Object key):

如果此映射包含指定鍵的映射關(guān)系,則返回 true。

5.boolean containsValue(Object value):

如果此映射將一個(gè)或多個(gè)鍵映射到指定值,則返回 true。

6.boolean isEmpty():

如果此映射未包含鍵-值映射關(guān)系,則返回 true。

7.int size():

返回此映射中的鍵-值映射關(guān)系數(shù)。

8.V get(Object key)

返回指定鍵所映射的值;如果此映射不包含該鍵的映射關(guān)系,則返回 null。

9.Set<K> keySet()

返回此映射中包含的鍵的 Set 視圖。

10.Collection<V> values()

返回此映射中包含的值的 Collection 視圖。

11.Set<Map.Entry<K,V>> entrySet()

返回此映射中包含的映射關(guān)系的 Set 視圖。

18.03 Map集合的基本功能測試

map.put("001", "旺財(cái)"); :第一次存儲(chǔ)直接存儲(chǔ)元素,返回null

map.put("001", "小強(qiáng)"); :不是第一次存儲(chǔ),用新值將以前的值替換掉,返回以前的值

map.remove("001"); :根據(jù)鍵刪除鍵值對元素,并返回鍵所對應(yīng)的值,沒有則返回空

18.04 Map集合的獲取功能測試
 1 Map<String, String> map = new HashMap<String, String>(); 2          3 map.put("001", "小明"); 4 map.put("002", "旺財(cái)"); 5 map.put("003", "小強(qiáng)"); 6 //根據(jù)鍵獲取值 7 System.out.

18.05 Map集合的遍歷之鍵找值

根據(jù)鍵找值:

獲取所有鍵的集合,遍歷鍵的集合,獲取到每一個(gè)鍵,根據(jù)鍵找值

 1 Map<String, String> map = new HashMap<String, String>(); 2 map.put("001", "小明"); 3 map.put("002", "旺財(cái)"); 4 map.put("003", "小強(qiáng)"); 5  6 //獲取所有的鍵 7 Set<String> set = map.keySet(); 8 for(String key : set) 9 {10     //根據(jù)鍵找值11     String value = map.get(key);12     System.out.println(key+":"+value);13 }

18.06 Map集合的遍歷之鍵值對對象找鍵和值

根據(jù)鍵值對對象找鍵和值:

獲取所有鍵值對對象的集合,遍歷鍵值對對象的集合,獲取到每一個(gè)鍵值對對象,根據(jù)鍵值對對象找鍵和值

 1 Map<String, String> map = new HashMap<String, String>(); 2 map.put("001", "小明"); 3 map.put("002", "旺財(cái)"); 4 map.put("003", "小強(qiáng)"); 5 //獲取所有鍵值對對象的集合 6 Set<Map.Entry<String, String>> set = map.entrySet(); 7 //遍歷鍵值對對象的集合,獲取每一個(gè)鍵值對對象 8 for(Map.Entry<String, String> me : set) 9 {10     //根據(jù)鍵值對對象獲取鍵和值11     String key = me.getKey();12     String value = me.getValue();13     System.out.println(key+":"+value);14 }

18.07 Map集合遍歷的兩種方式比較圖解

18.08 HashMap集合鍵是Stirng值是String的案例

HashMap類概述:鍵是哈希表結(jié)構(gòu),可以保證鍵的唯一性

 1 HashMap<String, String> map = new HashMap<String, String>(); 2          3 map.put("001", "小明"); 4 map.put("002", "旺財(cái)"); 5 map.put("003", "小強(qiáng)"); 6 map.put("004", "小紅"); 7 Set<String> set = map.keySet(); 8 for(String key : set) 9 {10     String value = map.get(key);11     System.out.println(key+"--"+value);12 }
18.09 HashMap集合鍵是Student值是String的案例
 1 // 創(chuàng)建集合對象 2 HashMap<Student, String> hm = new HashMap<Student, String>(); 3  4 hm.put(new Student("小明",23), "001"); 5 hm.put(new Student("小強(qiáng)",15), "002"); 6 hm.put(new Student("旺財(cái)",13), "003"); 7 hm.put(new Student("張三",17), "004"); 8 hm.put(new Student("小強(qiáng)",15), "005"); 9 10 // 遍歷11 Set<Student> set = hm.keySet();12 for (Student key : set) 13 {14     String value = hm.get(key);15     System.out.println(key.getName() + "---" + key.getAge() + "---"16             + value);17 }

當(dāng)鍵是自定義對象時(shí),需重寫hashCode()和equals()方法

18.10 LinkedHashMap的概述和使用

Map接口的哈希表和鏈接列表實(shí)現(xiàn),具有可預(yù)知的迭代順序。

由哈希表保證鍵的唯一性

由鏈表保證鍵的有序

 1 LinkedHashMap<String, String> hm = new LinkedHashMap<String,String>(); 2          3 // 創(chuàng)建并添加元素 4 hm.put("2345", "hello"); 5 hm.put("1234", "world"); 6 hm.put("3456", "java"); 7 hm.put("1234", "javaee"); 8 hm.put("3456", "android"); 9 10 // 遍歷11 Set<String> set = hm.keySet();12 for (String key : set) 13 {14     String value = hm.get(key);15     System.out.println(key + "---" + value);16 }

運(yùn)行結(jié)果:

2345---hello1234---javaee3456---android

18.11 TreeMap集合鍵是String值是String的案例

TreeMap類概述:鍵是紅黑樹結(jié)構(gòu),可以保證鍵的排序和唯一性

 1 // 創(chuàng)建集合對象 2 TreeMap<String, String> tm = new TreeMap<String, String>(); 3  4 // 創(chuàng)建元素并添加元素 5 tm.put("hello", "你好"); 6 tm.put("world", "世界"); 7 tm.put("java", "爪哇"); 8 tm.put("world", "世界2"); 9 tm.put("javaee", "爪哇EE");10 11 // 遍歷集合12 Set<String> set = tm.keySet();13 for (String key : set) 14 {15     String value = tm.get(key);16     System.out.println(key + "---" + value);17 }

運(yùn)行結(jié)果:

hello---你好java---爪哇javaee---爪哇EEworld---世界2

18.12 TreeMap集合鍵是Student值是String的案例

 1 public class Practice  2 { 3     public static void main(String[] args) 4     { 5         // 創(chuàng)建集合對象 6         TreeMap<Student, String> tm = new TreeMap<Student, String>(new Comparator<Student>()  7         { 8             @Override 9             public int compare(Student s1, Student s2) 10             {11                 // 主要條件12                 int num = s1.getAge() - s2.getAge();13                 // 次要條件14                 int num2 = num == 0 ? s1.getName().compareTo(15                         s2.getName()) : num;16                 return num2;17             }18         });19 20         tm.put(new Student("小明",23), "001");21         tm.put(new Student("小強(qiáng)",15), "002");22         tm.put(new Student("旺財(cái)",13), "003");23         tm.put(new Student("張三",17), "004");24         tm.put(new Student("小強(qiáng)",15), "005");25         26         Set<Map.Entry<Student, String>> set = tm.entrySet();27         for(Map.Entry<Student, String> me : set)28         {29             Student key = me.getKey();30             String value = me.getValue();31             System.out.println(key.getName()+":"+key.getAge()+":"+value);32         }33     }34 }

運(yùn)行結(jié)果:

旺財(cái):13:003小強(qiáng):15:005張三:17:004小明:23:001

18.13 統(tǒng)計(jì)字符串中每個(gè)字符出現(xiàn)的次數(shù)案例圖解

"aababcabcdabcde",獲取字符串中每一個(gè)字母出現(xiàn)的次數(shù)要求結(jié)果:a(5)b(4)c(3)d(2)e(1)

18.14 統(tǒng)計(jì)字符串中每個(gè)字符出現(xiàn)的次數(shù)案例代碼實(shí)現(xiàn)
 1 public class Practice  2 { 3     public static void main(String[] args) 4     { 5         // 定義一個(gè)字符串(改進(jìn)為鍵盤錄入) 6         Scanner sc = new Scanner(System.in); 7         System.out.println("請輸入一個(gè)字符串:"); 8         String line = sc.nextLine(); 9 10         // 定義一個(gè)TreeMap集合11         TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();12         13         //把字符串轉(zhuǎn)換為字符數(shù)組14         char[] chs = line.toCharArray();15         16         //遍歷字符數(shù)組,得到每一個(gè)字符17         for(char ch : chs)18         {19             //拿剛才得到的字符作為鍵到集合中去找值,看返回值20             Integer i =  tm.get(ch);21             22             //是null:說明該鍵不存在,就把該字符作為鍵,1作為值存儲(chǔ)23             if(i == null)24             {25                 tm.put(ch, 1);26             }27             else 28             {29                 //不是null:說明該鍵存在,就把值加1,然后重寫存儲(chǔ)該鍵和值30                 i++;31                 tm.put(ch,i);32             }33         }34         35         //定義字符串緩沖區(qū)變量36         StringBuilder sb=  new StringBuilder();37         38         //遍歷集合,得到鍵和值,進(jìn)行按照要求拼接39         Set<Character> set = tm.keySet();40         for(Character key : set)41         {42             Integer value = tm.get(key);43             sb.append(key).append("(").append(value).append(")");44         }45         46         //把字符串緩沖區(qū)轉(zhuǎn)換為字符串輸出47         String result = sb.toString();48         System.out.println("結(jié)果:"+result);49     }50 }

運(yùn)行結(jié)果:

請輸入一個(gè)字符串:aasdgrfedcsdf結(jié)果:a(2)c(1)d(3)e(1)f(2)g(1)r(1)s(2)

18.15 HashMap集合嵌套HashMap集合的案例

 1 public class Practice  2 { 3     public static void main(String[] args) 4     { 5         // 大集合對象 6         HashMap<String, HashMap<String, Integer>> bigMap = new HashMap<String, HashMap<String, Integer>>(); 7  8         // 集合對象1 9         HashMap<String, Integer> hm1 = new HashMap<String, Integer>();10         // 添加元素11         hm1.put("小明", 20);12         hm1.put("小強(qiáng)", 22);13         // 把集合添加到大集合14         bigMap.put("集合1", hm1);15 16         // 集合對象217         HashMap<String, Integer> hm2 = new HashMap<String, Integer>();18         // 添加元素19         hm2.put("旺財(cái)", 5);20         hm2.put("張三", 23);21         // 把集合添加到大集合22         bigMap.put("集合2", hm2);23         24         //遍歷集合25         Set<String> bigMapset = bigMap.keySet();26         for(String bigMapKey : bigMapset)27         {28             System.out.println(bigMapKey);29             HashMap<String, Integer> value = bigMap.get(bigMapKey);30             Set<String> set = value.keySet();31             for(String s : set)32             {33                 Integer i = value.get(s);34                 System.out.println("/t"+s+":"+i);35             }36         }37     }38 }

運(yùn)行結(jié)果:

集合1    小強(qiáng):22    小明:20集合2    旺財(cái):5    張三:23

18.16 集合多層嵌套的代碼體現(xiàn)

 1 public class Practice  2 { 3     public static void main(String[] args) 4     { 5         // 創(chuàng)建大集合 6         HashMap<String, HashMap<String, ArrayList<Student>>> czbkMap = new HashMap<String, HashMap<String, ArrayList<Student>>>(); 7  8         // 北京校區(qū)數(shù)據(jù) 9         HashMap<String, ArrayList<Student>> bjCzbkMap = new HashMap<String, ArrayList<Student>>();10         ArrayList<Student> array1 = new ArrayList<Student>();11         Student s1 = new Student("林青霞", 27);12         Student s2 = new Student("風(fēng)清揚(yáng)", 30);13         array1.add(s1);14         array1.add(s2);15         ArrayList<Student> array2 = new ArrayList<Student>();16         Student s3 = new Student("趙雅芝", 28);17         Student s4 = new Student("武鑫", 29);18         array2.add(s3);19         array2.add(s4);20         bjCzbkMap.put("基礎(chǔ)班", array1);21         bjCzbkMap.put("就業(yè)班", array2);22         czbkMap.put("北京校區(qū)", bjCzbkMap);23 24         // 西安校區(qū)數(shù)據(jù)25         HashMap<String, ArrayList<Student>> xaCzbkMap = new HashMap<String, ArrayList<Student>>();26         ArrayList<Student> array3 = new ArrayList<Student>();27         Student s5 = new Student("范冰冰", 27);28         Student s6 = new Student("劉意", 30);29         array3.add(s5);30         array3.add(s6);31         ArrayList<Student> array4 = new ArrayList<Student>();32         Student s7 = new Student("李冰冰", 28);33         Student s8 = new Student("張志豪", 29);34         array4.add(s7);35         array4.add(s8);36         xaCzbkMap.put("基礎(chǔ)班", array3);37         xaCzbkMap.put("就業(yè)班", array4);38         czbkMap.put("西安校區(qū)", xaCzbkMap);39 40         // 遍歷集合41         Set<String> czbkMapSet = czbkMap.keySet();42         for (String czbkMapKey : czbkMapSet) 43         {44             System.out.println(czbkMapKey);45             HashMap<String, ArrayList<Student>> czbkMapValue = czbkMap.get(czbkMapKey);46             Set<String> czbkMapValueSet = czbkMapValue.keySet();47             for (String czbkMapValueKey : czbkMapValueSet) 48             {49                 System.out.println("/t" + czbkMapValueKey);50                 ArrayList<Student> czbkMapValueValue = czbkMapValue.get(czbkMapValueKey);51                 for (Student s : czbkMapValueValue) 52                 {53                     System.out.println("/t/t" + s.getName() + "---"+ s.getAge());54                 }55             }56         }57     }58 }

運(yùn)行結(jié)果:

北京校區(qū)    就業(yè)班        趙雅芝---28        武鑫---29    基礎(chǔ)班        林青霞---27        風(fēng)清揚(yáng)---30西安校區(qū)    就業(yè)班        李冰冰---28        張志豪---29    基礎(chǔ)班        范冰冰---27        劉意---30

18.17 HashMap和Hashtable的區(qū)別

1:Hashtable和HashMap的區(qū)別

Hashtable:線程安全,效率低。不允許null鍵和null值

HashMap:線程不安全,效率高。允許null鍵和null值

2:List,Set,Map等接口是否都繼承子Map接口?

List,Set不是繼承自Map接口,它們繼承自Collection接口

Map接口本身就是一個(gè)頂層接口

18.18 Collections工具類的概述

Collections類概述:針對集合操作的工具類,都是靜態(tài)方法

Collection和Collections的區(qū)別:

Collection:是單列集合的頂層接口,有子接口List和Set。

Collections:是針對集合操作的工具類,有對集合進(jìn)行排序和二分查找的方法

18.19 Collections工具類的常見方法講解

Collections成員方法

1.public static <T extends Comparable<? super T>> void sort(List<T> list):

根據(jù)元素的自然順序?qū)χ付斜戆瓷蜻M(jìn)行排序。

2.public static <T> int binarySearch(List<? extends Comparable<? super T>> list,T key):

使用二分搜索法搜索指定列表,以獲得指定對象。

3.public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll):

根據(jù)元素的自然順序,返回給定 collection 的最大元素。

4.public static void reverse(List<?> list):

反轉(zhuǎn)指定列表中元素的順序。

5.public static void shuffle(List<?> list):

使用默認(rèn)隨機(jī)源對指定列表進(jìn)行置換。所有置換發(fā)生的可能性都是大致相等的。

例:

 1 public class Practice  2 { 3     public static void main(String[] args) 4     { 5         // 創(chuàng)建集合對象 6         List<Integer> list = new ArrayList<Integer>(); 7  8         // 添加元素 9         list.add(30);10         list.add(20);11         list.add(50);12         list.add(10);13         list.add(40);14 15         System.out.println("原始:" + list);16         //排序  默認(rèn)情況下是自然順序17          Collections.sort(list);18          System.out.println("排序:" + list);19         // 二分查找20         System.out.println("查找:"+Collections.binarySearch(list, 30));21         // 最大值22         System.out.println("最大值:"+Collections.max(list));23         // 反轉(zhuǎn)24         Collections.reverse(list);25         System.out.println("反轉(zhuǎn):" + list);26         //隨機(jī)置換27         Collections.shuffle(list);28         System.out.println("隨機(jī):" + list);29     }30 }

運(yùn)行結(jié)果:

原始:[30, 20, 50, 10, 40]排序:[10, 20, 30, 40, 50]查找:2最大值:50反轉(zhuǎn):[50, 40, 30, 20, 10]隨機(jī):[20, 10, 30, 50, 40]

18.20 ArrayList存儲(chǔ)自定義對象并排序案例

 1 public class Practice  2 { 3     public static void main(String[] args) 4     { 5         // 創(chuàng)建集合對象 6         List<Student> list = new ArrayList<Student>(); 7  8         // 創(chuàng)建學(xué)生對象 9         Student s1 = new Student("小明", 27);10         Student s2 = new Student("旺財(cái)", 30);11         Student s3 = new Student("小強(qiáng)", 28);12         Student s4 = new Student("張三", 29);13 14         // 添加元素對象15         list.add(s1);16         list.add(s2);17         list.add(s3);18         list.add(s4);19 20         // 排序21         // 自然排序必須實(shí)現(xiàn) Comparable 接口22         //Collections.sort(list);23         // 比較器排序24         // 如果同時(shí)有自然排序和比較器排序,以比較器排序?yàn)橹?5         Collections.sort(list, new Comparator<Student>() 26         {27             @Override28             public int compare(Student s1, Student s2) 29             {30                 int num = s2.getAge() - s1.getAge();31                 int num2 = num == 0 ? s1.getName().compareTo(s2.getName()): num;32                 return num2;33             }34         });35         // 遍歷集合36         for (Student s : list) 37         {38             System.out.println(s.getName() + "---" + s.getAge());39         }40     }41 }

運(yùn)行結(jié)果:

旺財(cái)---30張三---29小強(qiáng)---28小明---27

18.21 模擬斗地主洗牌和發(fā)牌

模擬斗地主洗牌和發(fā)牌

分析:

A:創(chuàng)建一個(gè)牌盒、B:裝牌、C:洗牌、D:發(fā)牌、E:看牌

 1 public class Practice  2 { 3     public static void main(String[] args) 4     { 5         // 創(chuàng)建一個(gè)牌盒 6         ArrayList<String> array = new ArrayList<String>(); 7  8         // 定義一個(gè)花色數(shù)組 9         String[] colors = { "♠", "♥", "♣", "♦" };10         // 定義一個(gè)點(diǎn)數(shù)數(shù)組11         String[] numbers = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10",12                 "J", "Q", "K" };13         // 裝牌14         for (String color : colors) 15         {16             for (String number : numbers) 17             {18                 array.add(color.concat(number));19             }20         }21         array.add("小王");22         array.add("大王");23 24         // 洗牌25         Collections.shuffle(array);26 27         // System.out.println("array:" + array);28 29         // 發(fā)牌30         ArrayList<String> player1 = new ArrayList<String>();31         ArrayList<String> player2 = new ArrayList<String>();32         ArrayList<String> player3 = new ArrayList<String>();33         ArrayList<String> dipai = new ArrayList<String>();34 35         for (int x = 0; x < array.size(); x++) 36         {37             if (x >= array.size() - 3) 38             {39                 diPai.add(array.get(x));40             } 41             else if (x % 3 == 0) 42             {43                 player1.add(array.get(x));44             } 45             else if (x % 3 == 1) 46             {47                 player2.add(array.get(x));48             } 49             else if (x % 3 == 2) 50             {51                 player3.add(array.get(x));52             }53         }54 55         // 看牌56         lookPoker("玩家1", player1);57         lookPoker("玩家2", player2);58         lookPoker("玩家3", player3);59 60         lookPoker("底牌", diPai);61     }62 63     public static void lookPoker(String name, ArrayList<String> array) 64     {65         System.out.print(name + "的牌是:");66         for (String s : array) 67         {68             System.out.print(s + " ");69         }70         System.out.println();71     }72 }

運(yùn)行結(jié)果:

玩家1的牌是:♦10 ♥4 ♠K ♠5 ♦6 ♥K ♦8 ♥2 ♥3 ♣8 ♣3 ♣4 ♦7 ♠Q ♣10 ♠3 ♠6 玩家2的牌是:♦9 ♦5 ♦A ♣5 ♦3 ♥8 ♣2 ♥J ♦J ♥5 ♦4 ♠9 ♠10 小王♣A ♥Q ♥7 玩家3的牌是:♠A ♣6 ♦2 ♦K ♣9 ♣K ♣7 ♦Q ♠4 ♥9 ♠7 大王♣Q ♥10 ♠8 ♣J ♠2 底牌的牌是:♥A ♥6 ♠J 

18.22 模擬斗地主洗牌和發(fā)牌并對牌進(jìn)行排序的代碼實(shí)現(xiàn)

思路:

A:創(chuàng)建一個(gè)HashMap集合

B:創(chuàng)建一個(gè)ArrayList集合

C:創(chuàng)建花色數(shù)組和點(diǎn)數(shù)數(shù)組

D:從0開始往HashMap里面存儲(chǔ)編號(hào),并存儲(chǔ)對應(yīng)的牌,同時(shí)往ArrayList里面存儲(chǔ)編號(hào)即可。

E:洗牌(洗的是編號(hào))

F:發(fā)牌(發(fā)的也是編號(hào),為了保證編號(hào)是排序的,就創(chuàng)建TreeSet集合接收)

G:看牌(遍歷TreeSet集合,獲取編號(hào),到HashMap集合找對應(yīng)的牌)

 1 public class Practice  2 { 3     public static void main(String[] args) 4     { 5         // 創(chuàng)建一個(gè)HashMap集合 6         HashMap<Integer, String> hm = new HashMap<Integer, String>(); 7  8         // 創(chuàng)建一個(gè)ArrayList集合 9         ArrayList<Integer> array = new ArrayList<Integer>();10 11         // 創(chuàng)建花色數(shù)組和點(diǎn)數(shù)數(shù)組12         // 定義一個(gè)花色數(shù)組13         String[] colors = { "♠", "♥", "♣", "♦" };14         // 定義一個(gè)點(diǎn)數(shù)數(shù)組15         String[] numbers = { "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q",16                 "K", "A", "2", };17 18         // 從0開始往HashMap里面存儲(chǔ)編號(hào),并存儲(chǔ)對應(yīng)的牌,同時(shí)往ArrayList里面存儲(chǔ)編號(hào)即可。19         int index = 0;20 21         for (String number : numbers) 22         {23             for (String color : colors) 24             {25                 String poker = color.concat(number);26                 hm.put(index, poker);27                 array.add(index);28                 index++;29             }30         }31         hm.put(index, "小王");32         array.add(index);33         index++;34         hm.put(index, "大王");35         array.add(index);36 37         // 洗牌(洗的是編號(hào))38         Collections.shuffle(array);39 40         // 發(fā)牌(發(fā)的也是編號(hào),為了保證編號(hào)是排序的,就創(chuàng)建TreeSet集合接收)41         TreeSet<Integer> player1 = new TreeSet<Integer>();42         TreeSet<Integer> player2 = new TreeSet<Integer>();43         TreeSet<Integer> player3 = new TreeSet<Integer>();44         TreeSet<Integer> diPai = new TreeSet<Integer>();45 46         for (int x = 0; x < array.size(); x++) 47         {48             if (x >= array.size() - 3) 49             {50                 diPai.add(array.get(x));51             } else if (x % 3 == 0) 52             {53                 player1.add(array.get(x));54             } else if (x % 3 == 1) 55             {56                 player2.add(array.get(x));57             } else if (x % 3 == 2) 58             {59                 player3.add(array.get(x));60             }61         }62 63         // 看牌(遍歷TreeSet集合,獲取編號(hào),到HashMap集合找對應(yīng)的牌)64         lookPoker("玩家1", player1, hm);65         lookPoker("玩家2", player2, hm);66         lookPoker("玩家3", player3, hm);67         lookPoker("底牌", diPai, hm);68     }69 70     // 寫看牌的功能71     public static void lookPoker(String name, TreeSet<Integer> ts,72             HashMap<Integer, String> hm) 73     {74         System.out.print(name + "的牌是:");75         for (Integer key : ts) 76         {77             String value = hm.get(key);78             System.out.print(value + " ");79         }80         System.out.println();81     }82 }

運(yùn)行結(jié)果:

玩家1的牌是:♠3 ♦3 ♥4 ♣4 ♥5 ♣5 ♠6 ♣8 ♥9 ♦10 ♥J ♣J ♦Q ♣A ♠2 ♣2 大王玩家2的牌是:♠5 ♦5 ♣7 ♦7 ♠9 ♥10 ♦J ♠Q ♣Q ♠K ♣K ♠A ♥A ♦A ♥2 ♦2 小王玩家3的牌是:♥3 ♣3 ♠4 ♦4 ♥6 ♣6 ♠7 ♥7 ♥8 ♦8 ♣9 ♠10 ♣10 ♠J ♥Q ♥K ♦K 底牌的牌是:♦6 ♠8 ♦9 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄色特级视频 | 午夜精品在线视频 | 国产69精品久久久久99尤 | 91精品观看91久久久久久国产 | 成人在线观看污 | 日本最新免费二区三区 | 欧美精品一区二区久久 | 国av在线 | 免费在线观看成人av | 一级毛片免费观看在线 | 亚洲一区在线免费视频 | 男人的天堂色偷偷 | 在线成人一区二区 | 毛片三区 | 日韩黄色三级视频 | 国产精品区在线12p 午夜视频色 | 国产精品久久久久久久久久久久午夜 | 激情亚洲一区二区三区 | www.99久久久| 精品不卡| 一级毛片免费大片 | 国产精品免费大片 | 亚洲啪啪| 欧美成人精品一区二区男人小说 | 欧美成人午夜一区二区三区 | 欧美久久久久久久久 | 色悠悠久久久久 | 精品国产91一区二区三区 | 羞羞答答视频 | 天堂二区| pornoⅹxxxxhd麻豆 | 精品亚洲视频在线 | 私库av在线免费观看 | 一区二区视频在线看 | 国产成人免费精品 | 第一区免费在线观看 | www.91视频com| 国产精品伊人久久 | 亚洲国产精品500在线观看 | 久草在线观看资源 | 在线观看免费毛片视频 |