日期:2014-05-20 浏览次数:20897 次
public class test6 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("a", null);
map.put("b", "马先生");
map.put("c", "马先生");
map.put("1", "马先生");
map.put("2", "马先生");
map.put("3", "马先生");
map.put("4", "马先生");
Set i = map.keySet();
//set集合应该为[3, 2, 1, b, c, 4, a]
System.out.println(i);
int len = i.size(); //i 为7
//获得set的集合长度 随机该长度下表的值
System.out.println(len);
Random random = new Random();
int key =random.nextInt(len); // key 为0到7之间任意的随机数
?????????
如何获得set集合中 随机出来的那个下标的值
通过得到的随机下标值如何得到 "a " 或者 "b"
??????????
}
Map map = new HashMap();
map.put("a", null);
map.put("b", "马先生");
map.put("c", "张先生");
map.put("1", "李先生");
map.put("2", "余先生");
map.put("3", "P先生");
map.put("4", "SB先生");
Set<Map.Entry> set = map.entrySet();
Object[] entryArray = set.toArray();//谁能告诉我这步直接转换成Map.Entry[]数组,强制(Map.Entry[]) set.toArray()报错
for (int i = 0; i < entryArray.length; i++) {
Map.Entry entry=(Map.Entry)entryArray[i];
System.out.println("i:"+i+"key:"+entry.getKey() + ":"
+ (entry.getValue()==null?"":entry.getValue()));
}
Random random = new Random();
int key = random.nextInt(set.size());
Map.Entry entry=(Map.Entry)entryArray[key];
System.out.println(entry.getKey() + ":"
+ entry.getValue());