hashmap集合+ArrayList集合+Collections集合工具类shuffle()和sort()
hashmap中get(key)、put(key/value)
Arraylist中的add()、get(下标值),此时ArrayLIST集合相当于hashmap中的set集合,可以使用迭代器或者增强for遍历内容
或者不使用Arraylist,直接使用entryset得到Map.entry<E>,使用getkey和getvalue得到key值
1 package com.oracle.demo01;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; public class Doudizhu {
public static void main(String[] args) {
//定义扑克牌Map 默认按照key值从小到大排序
HashMap<Integer,String> pooker=new HashMap<Integer,String>();
//定义装有扑克牌号的集合 单独的与map集合数字一致2
ArrayList<Integer> pookerNum=new ArrayList<Integer>();
//封装数据 4种花色(13个牌)+大小王
String[] color={"♡","","♧","◇"};//点击选择utf-8保存
String[] number={"","A","K","Q","J","","","","","","","",""};
//嵌套for循环,先数字,后花色
int index=;
for(String n:number){
for(String c:color){
pooker.put(index,c+n);
pookerNum.add(index);
index++;
}
}
//封装大小王
pooker.put(, "大王");
pooker.put(, "小王");
pookerNum.add();
pookerNum.add(); //测试
// System.out.println(pooker);
//洗牌
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=;i<pookerNum.size();i++){
if(i<){
bottom.add(pookerNum.get(i));
}else if(i%==){
player1.add(pookerNum.get(i));
}else if(i%==){
player2.add(pookerNum.get(i));
}else if(i%==){
player3.add(pookerNum.get(i));
}
}
//排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
Collections.sort(bottom);
//遍历看牌
System.out.println();
look("",pooker,player1);
look("",pooker,player2);
look("",pooker,player3);
look("底牌",pooker,bottom);
}
//通用的方法
public static void look(String Name,HashMap<Integer,String> pooker,
ArrayList<Integer> bottom){
System.out.print(Name+":");
for(Integer number:bottom){
System.out.print(pooker.get(number)+" ");
}
System.out.println();
}
}
package com.oracle.demo01; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator; public class DouDiZhu3 {
public static void main(String[] args) {
//定义map集合
HashMap<Integer,String> big=
new HashMap<Integer,String>();
ArrayList<Integer> arr=new ArrayList<Integer>();
//封装数据,通过内外循环,拼接字符串
String[] arr1={"♣","◇","♠","♥"};
String[] arr2={"","A","K","Q","J","","","","","","","",""};
//拼接字符串,并存入map集合中
int index=;
for(String r2:arr2){
for(String r1:arr1){
//必须创建不同的对象,进行存储
arr.add(index);
//System.out.println(arr);
big.put(index, r1+r2);
/* //对arr进行整体的清空
arr.clear();*/
index++;
}
}
//封装大小王
big.put(, "大王");
big.put(, "小王");
arr.add();
arr.add();
//此时为一个对象,会出现值覆盖问题
/*big.put(new ArrayList<Integer>(0), "大王");
big.put(new ArrayList<Integer>(1), "小王");*/
//测试是否存储成功,map自己调用tostring()
System.out.print(big);
//洗牌
Collections.shuffle(arr);
//创建四个容器
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=;i<arr.size();i++){
if(i<){
bottom.add(arr.get(i));
}else if(i%==){
player1.add(arr.get(i));
}else if(i%==){
player2.add(arr.get(i));
}else if(i%==){
player3.add(arr.get(i));
}
}
//排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
Collections.sort(bottom);
//遍历看牌
System.out.println();
look("",big,player1);
look("",big,player2);
look("",big,player3);
look("底牌",big,bottom);
}
//通用的方法
public static void look(String Name,HashMap<Integer,String> big,
ArrayList<Integer> bottom){
System.out.print(Name+":");
//创建迭代器对象 数据类型与要遍历的集合数据类型一致
Iterator<Integer> i=bottom.iterator();
while(i.hasNext()){
int ii=i.next();//对象调用
System.out.print(big.get(ii)+" ");
}
System.out.println();
}
}