首页 技术 正文
技术 2022年11月14日
0 收藏 559 点赞 3,435 浏览 3488 个字
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();
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902