洗牌数字地图中的和中选择一个。 [英] Shuffling numbers in a map and selecting one.
本文介绍了洗牌数字地图中的和中选择一个。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一组固定的号码从1至5。这些被添加到地图如下:
I have a group of fixed numbers from 1 to 5. These are added into a Map as follows:
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
map.put(4, 4);
map.put(5, 5);
这是我随意拿,我从取出两个数地图<整数,整数GT;
留下三个数字
From that I randomly take two numbers that I remove from the Map<Integer, Integer>
leaving three numbers.
map.remove(Rwiner);
map.remove(Rsec);
这会离开我的三个数字像2,1,4。我想挑选出从一个随机数。我应该怎么办?
This would leave me three numbers like 2,1,4. I want to pick out a random number from that. What should i do?
推荐答案
这样的事情也许是:
// Setup
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(1, 1);
map.put(2, 2);
map.put(3, 3);
map.put(4, 4);
map.put(5, 5);
int Rwiner = 3, Rsec = 5;
map.remove(Rwiner);
map.remove(Rsec);
// Get random entry from current map.
Random rnd = new Random();
int randomIndex = rnd.nextInt(map.size());
int randomKey = new ArrayList<Integer>(map.keySet()).get(randomIndex);
System.out.printf("Random entry: %d -> %d", randomKey, map.get(randomKey));
这篇关于洗牌数字地图中的和中选择一个。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文