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();
}
}