黑马程序员——Java学习之扑克牌程序分析

来源:互联网 发布:淘宝按价格排序不准了 编辑:程序博客网 时间:2024/05/01 18:59

——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-

学完集合类这一章总感觉自己脑袋有些糊涂,所以借助一个小程序来分析分析、扒一扒这些不一般的集合兄弟们。

我们如何选择哪种集合?
1.需不需要键值对:
需要:Map
无序:HashMap
有序:LinkedHashMap
排序:TreeMap
不需要:Collection
2.使用Collection
是否需要有序?还要综合其他的方面:是否需要经常性的新增、删除、查找
有序:
List
新增、删除?:链表
查找?数组
无序:
Set
新增、删除?:链表
查找?数组

模拟斗地主洗牌和发牌
思路:
1.组装一副牌:
1).四个花色:红桃,黑桃,梅花,方片–>花色的集合
2).A,2,3,4,5,6,7,8,9,10,J,Q,K,大王,小王–>数字的集合
2.生成54张牌,装到一个ArrayList中;
3.洗牌:Collections.shuffle();
4.发牌:三个人玩,三张底牌
5.打印每个人的牌;

package com.mytest.demo24_模拟斗地主洗牌和发牌;import java.util.ArrayList;import java.util.Collections;public class Demo {    public static void main(String[] args) {        //组装一副牌;        //1.花色的集合        String[] colors = {"♥","♠","♣","♢"};        //2.数字        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};        //一副牌的集合        ArrayList<String> pokerList = new ArrayList<String>();        for(String c : colors){            for(String n : numbers){                pokerList.add(c + n);            }        }        //大王小王        pokerList.add("大王");        pokerList.add("小王");        //3.洗牌        Collections.shuffle(pokerList);        //4.发牌;三个人,一个底牌        ArrayList<String> user1List = new ArrayList<String>();        ArrayList<String> user2List = new ArrayList<String>();        ArrayList<String> user3List = new ArrayList<String>();        ArrayList<String> dipaiList = new ArrayList<String>();        for(int i = 0 ; i < pokerList.size() ;i++){            if(i >= pokerList.size() - 3){                dipaiList.add(pokerList.get(i));            }            else if(i % 3 == 0){                user1List.add(pokerList.get(i));            }else if(i % 3 == 1){                user2List.add(pokerList.get(i));            }else if(i % 3 == 2){                user3List.add(pokerList.get(i));            }        }        System.out.println("邓超:" + user1List);        System.out.println("郑恺:" + user2List);        System.out.println("AngelaBaby:" + user3List);        System.out.println("底牌:" + dipaiList);    }}

思路:

1.封装一副牌;
花色的数组:
数字的数组:
2.将封装的牌,存储到HashMap中,将编号作为键,将扑克作为值;
3.同时,生成一个存储扑克牌编号的集合ArrayList。存储1–52的编号;
4.洗牌:洗编号;
5.发牌:发编号;
6.看牌:通过编号,到HashMap中找到对应的扑克,显示;

package com.mytest.demo25_斗地主将牌排序;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.TreeSet;public class Demo {    public static void main(String[] args) {        //1.定义一个HashMap,存储扑克牌和编号        HashMap<Integer,String> pokerMap = new HashMap<Integer,String>();        //2.定义一个ArrayList,存储扑克的编号        ArrayList<Integer> pokerIndexList = new ArrayList<Integer>();        //3.        //  数字的数组        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};        //定义花色的数组:        String[] colors = {"♥","♠","♣","♢"};        //3.封装        int index = 1;        for(String n : numbers){            for(String c : colors){                //将编号和扑克添加到HashMap                pokerMap.put(index, c + n);                //将编号添加到编号的集合                pokerIndexList.add(index);                index++;            }        }        //大小王        pokerMap.put(index, "小王");        pokerIndexList.add(index);        index++;        pokerMap.put(index,"大王");        pokerIndexList.add(index);        //洗牌,洗编号        Collections.shuffle(pokerIndexList);        TreeSet<Integer> user1List = new TreeSet<Integer>();        TreeSet<Integer> user2List = new TreeSet<Integer>();        TreeSet<Integer> user3List = new TreeSet<Integer>();        TreeSet<Integer> dipaiList = new TreeSet<Integer>();        //发牌,发的是编号        for(int i = 0 ;i < pokerIndexList.size() ;i ++){            if(i >= pokerIndexList.size() - 3){                dipaiList.add(pokerIndexList.get(i));            }else if(i % 3 == 0){                user1List.add(pokerIndexList.get(i));            }else if(i % 3 == 1){                user2List.add(pokerIndexList.get(i));            }else if(i % 3 == 2){                user3List.add(pokerIndexList.get(i));            }        }        //第一个人的牌        System.out.print("邓超:");        for(Integer n : user1List){            System.out.print(pokerMap.get(n) + " ,");        }        System.out.println();        System.out.print("郑恺:");        for(Integer n : user2List){            System.out.print(pokerMap.get(n) + " ,");        }        System.out.println();        System.out.print("AngelaBaby:");        for(Integer n : user3List){            System.out.print(pokerMap.get(n) + " ,");        }        System.out.println();        System.out.print("底牌:");        for(Integer n : dipaiList){            System.out.print(pokerMap.get(n) + " ,");        }    }}

总结:
Collection:
|–List:有序,允许有重复值
ArrayList:数组实现;不保证线程安全,效率高
Vector:数组实现;线程安全,效率低
LinkedList:链表实现;不保证线程安全,效率高;
|–Set:无序,不允许有重复值
HashSet:哈希表实现;无序
LinkedHashSet:链表哈希表;有序
TreeSet:二叉树实现:被排序的
Map:键值对存储,键不能重复。值无所谓
|–HashMap:
|–LinkedHashMap:
|–TreeMap:
|–Hashtable:

数据结构:
数组:查询快,随机获取元素快。添加和删除慢;
链表:查询慢。添加和删除快
哈希表:查询块,添加,删除都快,它综合了数组和链表的优点;
树:具有排序的特性;自然排序和比较器

——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-

0 0