viranc 2017-03-31 14:53 采纳率: 0%
浏览 1997

请问arraylist的元素值跟hashmap的索引值一样就能够通过元素值获取到hashmap的?

请问arraylist的元素值跟hashmap的索引值一样就能够通过元素值获取到hashmap的key跟value呢??
两个索引相同能获取??
求解答

HashMap hm = new HashMap<>(); //存储索引和扑克牌
ArrayList list = new ArrayList<>(); //存储索引
--------------------代码如下--------------
//买一副牌
String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
String[] color = {"方片","梅花","红桃","黑桃"};
HashMap hm = new HashMap<>(); //存储索引和扑克牌
ArrayList list = new ArrayList<>(); //存储索引
int index = 0; //索引的开始值
for(String s1 : num) {
for(String s2 : color) {
hm.put(index, s2.concat(s1)); //将索引和扑克牌添加到HashMap中
list.add(index); //将索引添加到ArrayList集合中
index++;
}
}
hm.put(index, "小王");
list.add(index);
index++;
hm.put(index, "大王");
list.add(index);
//洗牌
Collections.shuffle(list);
//发牌
TreeSet gaojin = new TreeSet<>();
TreeSet longwu = new TreeSet<>();
TreeSet me = new TreeSet<>();
TreeSet dipai = new TreeSet<>();

for(int i = 0; i < list.size(); i++) {
    if(i >= list.size() - 3) {
        dipai.add(list.get(i));                         //将list集合中的索引添加到TreeSet集合中会自动排序
    }else if(i % 3 == 0) {
        gaojin.add(list.get(i));
    }else if(i % 3 == 1) {
        longwu.add(list.get(i));
    }else {
        me.add(list.get(i));
    }
}

//看牌
lookPoker("A", gaojin, hm);
lookPoker("B", longwu, hm);
lookPoker("C", me, hm);
lookPoker("底牌", dipai, hm);

}

public static void lookPoker(String name,TreeSet ts,HashMap hm) {
System.out.print(name + "的牌是:");
for (Integer index : ts) {
System.out.print(hm.get(index) + " ");
}

System.out.println();
  • 写回答

2条回答 默认 最新

  • bug斗罗 2017-03-31 15:25
    关注

    为什么用弄个ArrayList,没有意义

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘