Collections集合工具类,集合嵌套,集合综合案例斗地主
1 Collections集合工具类
(可以对比Arrays工具类共同记忆)
常用方法:

例:
import java.util.ArrayList;
import java.util.Collections; public class CollectionsTest {
public static void main(String[] args) {
ArrayList<Integer> arr=new ArrayList<Integer>();
arr.add(5);
arr.add(7);
arr.add(1);
arr.add(3);
arr.add(2);
arr.add(8);
arr.add(9); //排序
Collections.sort(arr);
for(int i:arr){
System.out.print(i+" ");
} System.out.println("");
System.out.println(""); //打乱
Collections.shuffle(arr);
for(int i:arr){
System.out.print(i+" ");
}
}
}

2集合嵌套
1)ArrayList嵌套 ArrayList
ArrayList< ArrayList<String> >
Collection< ArrayList<Integer> >
2)Map嵌套 ArrayList
HashMap<String, ArrayList<Person>>
ArrayList< HashMap<String, String>>
3)Map集合嵌套
HashMap<String, HashMap<String,String>>
HashMap<String, HashMap<Person,String>>
例:
import java.util.HashMap;
import java.util.Set;
import demo01.Person; public class Test1 {
//嵌套
public static void main(String[] args) {
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("小丸子",18));
java0601.put("003", new Person("狼",18)); HashMap<String,Person> java0929=new HashMap<String,Person>();
java0929.put("001", new Person("小红",18));
java0929.put("002", new Person("小兰",18));
java0929.put("003", new Person("小明",18)); oracle.put("java0601",java0601);
oracle.put("java0929",java0929); //遍历(增强for+keyset)
Set<String> classes=oracle.keySet(); //获取班级名称
for(String clas:classes){
//通过每个班级名获取该班的所有人的信息HashMap
HashMap<String,Person> c=oracle.get(clas);
//获取所有学生的学号
Set<String> snos=c.keySet();
//遍历
for(String sno:snos){
Person p=c.get(sno);
System.out.println("班级为:"+clas+"学号为:"+sno+"的学生信息为"+p);
}
}
}
}

3集合继承体系的面向对象思想

1)接口:用来明确所有集合中该具有的功能,相当于在定义集合功能标准;
2)抽象类:把多个集合中功能实现方式相同的方法,抽取到抽象类实现,具体集合不再编写,继承使用即可;
3)具体类:继承抽象类,实现接口,重写所有抽象方法,达到具备指定功能的集合。每个具体集合类,根据自身的数据存储结构方式,对接口中的功能方法,进行不同方式的实现。
4模拟斗地主洗牌发牌

规则:
1)组装54张扑克牌
2)将54张牌顺序打乱
3)三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌。
4)查看三人各自手中的牌(按照牌的大小排序)、底牌
5)手中扑克牌从大到小的摆放顺序:大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3
分析:
1)准备牌:
完成数字与纸牌的映射关系:
使用双列Map(HashMap)集合,完成一个数字与字符串纸牌的对应关系(相当于一个字典)。
2)洗牌:
通过数字完成洗牌发牌
3)发牌:
将每个人以及底牌设计为ArrayList<String>,将最后3张牌直接存放于底牌,剩余牌通过对3取模依次发牌。
存放的过程中要求数字大小与斗地主规则的大小对应。
将代表不同纸牌的数字分配给不同的玩家与底牌。
4)看牌:
通过Map集合找到对应字符展示。
通过查询纸牌与数字的对应关系,由数字转成纸牌字符串再进行展示。
代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; public class Game {
public static void main(String[] args) {
HashMap<Integer,String> pookerMap=new HashMap<Integer,String>(); //存排
ArrayList<Integer> pookerNum=new ArrayList<Integer>(); //存排的下标 //准备牌
String[] number={"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
String[] color={"♥","♦","♣","♠"}; int index=2; //0和1留给大小王
//遍历牌号
for(String num:number){
for(String col:color){
//向map中添加牌
pookerMap.put(index, col+num);
//向集合中添加索引
pookerNum.add(index);
index++;
}
}
//System.out.println(pookerMap); //添加大小王
pookerMap.put(0, "大王");
pookerNum.add(0);
pookerMap.put(1, "小王");
pookerNum.add(1); //洗牌
Collections.shuffle(pookerNum); //创建四个容器
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<pookerNum.size();i++){
//先发三张底牌
if(i<3){
bottom.add(pookerNum.get(i));
}else if(i%3==0){
player1.add(pookerNum.get(i));
}else if(i%3==1){
player2.add(pookerNum.get(i));
}else if(i%3==2){
player3.add(pookerNum.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();
}
}

Collections集合工具类,集合嵌套,集合综合案例斗地主的更多相关文章
- Collections 集合工具类
集合工具类 包括很多静态方法来操作集合list 而Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素进行排序.搜索以及线程安全等各种操作. 1) 排序( ...
- 集合-强大的集合工具类:java.util.Collections中未包含的集合工具
任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collections包含的工具方法.Guava沿着这些路线提供了更多的工具方法:适用于所有集合的静态方法.这是Guava最流行和成熟 ...
- java之集合工具类Collections
Collections类简介 java.utils.Collections 是集合工具类,用来对集合进行操作.此类完全由在 collection 上进行操作或返回 collection 的静态方法组成 ...
- 005-guava 集合-集合工具类-java.util.Collections中未包含的集合工具[Maps,Lists,Sets],Iterables、Multisets、Multimaps、Tables
一.概述 工具类与特定集合接口的对应关系归纳如下: 集合接口 属于JDK还是Guava 对应的Guava工具类 Collection JDK Collections2:不要和java.util.Col ...
- [Google Guava] 2.3-强大的集合工具类:java.util.Collections中未包含的集合工具
原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collections包含的工具方法.G ...
- java第十九天,Collections集合工具类的使用
Collections Java中集合都实现了Collection接口,那么针对集合一些特定的功能,有没有一个接口或类能够统一的集成一些集合必要的功能呢?当然能.它就是--Collections集合工 ...
- Collections集合工具类常用的方法
java.utils.Collections //是集合工具类,用来对集合进行操作.部分方法如下: public static <T> boolean addAll(Collection& ...
- [Google Guava] 强大的集合工具类:java.util.Collections中未包含的集合工具
转载的,有问题请联系我 原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collecti ...
- Collections集合工具类的常用方法
Collections集合工具类的方法 addAll与shuffle import java.util.ArrayList; import java.util.Collections; /* - ja ...
随机推荐
- bzoj 2216: Lightning Conductor 单调队列优化dp
题目大意 已知一个长度为\(n\)的序列\(a_1,a_2,...,a_n\)对于每个\(1\leq i\leq n\),找到最小的非负整数\(p\)满足: 对于任意的\(j\), \(a_j \le ...
- Docker入门(四):服务(Services)
这个<Docker入门系列>文档,是根据Docker官网(https://docs.docker.com)的帮助文档大致翻译而成.主要是作为个人学习记录.有错误的地方,Robin欢迎大家指 ...
- Divide Two Integers-不用'/' '*' '%'操作实现整数的除法
题目描述: 不用 '*' '/' 和 '%' 运算实现两个整数的除法 题目来源:http://oj.leetcode.com/problems/divide-two-integers/ 题目分析: 例 ...
- Unity3d笔记
当变量重命名后,已序列化保存的值会丢失,如果希望继续保留其数值,可使用FormerlySerializedAs,如下代码所示: [UnityEngine.Serialization.FormerlyS ...
- CodeForces 467D(267Div2-D)Fedor and Essay (排序+dfs)
D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 微信小程序开发之带搜索记录的搜索框
实现功能:点击搜索框,有搜索记录时以下拉菜单显示,点击下拉子菜单,将数据赋值到搜索框,点击搜索图标搜索,支持清空历史记录,可手动输入和清空查询关键字, UI: wxml: <!--查询历史记录数 ...
- 删除重复Row记录数据
使用CTE,ROW_NUMBER,PARTITION BY来处理数据表重复记录. 先准备下面的数据: IF OBJECT_ID('tempdb.dbo.#Part') IS NOT NULL DROP ...
- js读取excel中日期格式转换问题
在使用js-xlsx插件来读取excel时,会将2018/10/16这种数据自动装换成48264.12584511. 所以需要自己手动再转换回来 // excel读取2018/01/01这种时间格式是 ...
- encodeURI和uncodeURIComponent的介绍
encodeURI.decodeURI encodeURI.decodeURI 对字符转义:不替换特殊字符有18个.(大小写)字母.数字. 替换目标 将字符替换为 HTML URL编码 替换范围 A- ...
- aimOffset注意事项
AimOffset的记录 AimOffset是什么,就是动画(相对于某个具体姿势比如待机动作的)叠加. AimOffset有什么用,简单说就是叠加动作,比如无双中骑马挥刀动作叠加. 注意步骤 1所有分 ...