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集合工具类,集合嵌套,集合综合案例斗地主的更多相关文章

  1. Collections 集合工具类

    集合工具类  包括很多静态方法来操作集合list 而Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素进行排序.搜索以及线程安全等各种操作. 1) 排序( ...

  2. 集合-强大的集合工具类:java.util.Collections中未包含的集合工具

    任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collections包含的工具方法.Guava沿着这些路线提供了更多的工具方法:适用于所有集合的静态方法.这是Guava最流行和成熟 ...

  3. java之集合工具类Collections

    Collections类简介 java.utils.Collections 是集合工具类,用来对集合进行操作.此类完全由在 collection 上进行操作或返回 collection 的静态方法组成 ...

  4. 005-guava 集合-集合工具类-java.util.Collections中未包含的集合工具[Maps,Lists,Sets],Iterables、Multisets、Multimaps、Tables

    一.概述 工具类与特定集合接口的对应关系归纳如下: 集合接口 属于JDK还是Guava 对应的Guava工具类 Collection JDK Collections2:不要和java.util.Col ...

  5. [Google Guava] 2.3-强大的集合工具类:java.util.Collections中未包含的集合工具

    原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collections包含的工具方法.G ...

  6. java第十九天,Collections集合工具类的使用

    Collections Java中集合都实现了Collection接口,那么针对集合一些特定的功能,有没有一个接口或类能够统一的集成一些集合必要的功能呢?当然能.它就是--Collections集合工 ...

  7. Collections集合工具类常用的方法

    java.utils.Collections //是集合工具类,用来对集合进行操作.部分方法如下: public static <T> boolean addAll(Collection& ...

  8. [Google Guava] 强大的集合工具类:java.util.Collections中未包含的集合工具

    转载的,有问题请联系我 原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collecti ...

  9. Collections集合工具类的常用方法

    Collections集合工具类的方法 addAll与shuffle import java.util.ArrayList; import java.util.Collections; /* - ja ...

随机推荐

  1. Codeforces round 396(Div. 2) 题解

    Problem A 题目大意 给定两个字符串,要求构造出一个最长的一个串满足:这个串是其中一个串的字序列并且不是另一个串的子序列.输出长度.\((len \leq 10^5)\) 题解 千万年死在读题 ...

  2. bzoj 4275 Badania naukowe —— DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4275 枚举 \( C \) 在 \( A \) 和 \( B \) 中的位置,然后取它前后的 ...

  3. 为BindingList添加Sort

    最近在优化WPF性能时, 发现在特定条件下BindingList比ObservableCollection性能更高, 因为它提供Disable/Enable 更改通知的方法.这样我们可以不需要很频繁的 ...

  4. openStack cinder 在往虚拟机上挂载云磁盘时总是提示挂在错误 最后找到原因原来是指定挂载云磁盘的虚拟机被锁定

    openStack 虚拟机的锁定功能是一个为了保护虚拟机 被误删除的一项创新共! 在VMs锁定状态下,一大部分针对此锁定的虚拟机都是无法执行的!! 需要进行相应的操作前,请注意解锁指定虚拟机,操作完成 ...

  5. deleteMany is not a function

    问题: 同事使用了deleteMany方法用于删除数据,但是全公司只有我一个人报错deleteMany is not a function. 很自然,输出了model.deleteMany,得到的结果 ...

  6. win7+64位+Oracle+11g+64位下使用P…

    1)安装Oracle 11g 64位   2)安装32位的Oracle客户端( instantclient-basic-win32-11.2.0.1.0)   下载instantclient-basi ...

  7. IIS 6.0曝远程代码执行漏洞CVE-2017-7269

    一.漏洞说明 漏洞编号:CVE2017-7269 影响中间件:IIS6.0 影响服务器版本:windows 2003 R2 二. 环境搭建 虚拟机kali : 192.168.1.2 靶机window ...

  8. HDU - 5094 Maze(状压+bfs)

    Maze This story happened on the background of Star Trek. Spock, the deputy captain of Starship Enter ...

  9. POJ - 2349 ZOJ - 1914 Arctic Network 贪心+Kru

    Arctic Network The Department of National Defence (DND) wishes to connect several northern outposts ...

  10. C++11之lambda表达式应用

    应用 foreach语句中 #include <time.h> #include <algorithm> using namespace std; void func(int ...