我试图找出要使用的正确地图或列表......
我需要将值添加到列表或地图中,然后随机检索...
像
这样的东西Map<String, String>map
index 1 of map (or list) = "John", "John is from some country"
index 2 of map (or list) = "Mary", "Mary is from some country"
index 3 of map (or list) = "Paul", "Paul is from some country"
然后将索引的值传递给变量...
索引1的结果
String name = "John"
String from = "John is from some country"
欢迎任何想法......
答案 0 :(得分:0)
试试这个
public String randomMap(){
Map<String,String> test=new TreeMap<String, String>();
Object[] arraySet=test.keySet().toArray();
Random randomGenerator = new Random();
long range = (long) 1- (long) arraySet.length;
long fraction = (long) (range * randomGenerator.nextDouble());
int randomNumber = (int) (fraction + 1);
String setToGet=(String)arraySet[Math.abs(randomNumber)] ;
String x=test.get(setToGet);
return (setToGet+x);
}
我的想法是更改数组中的键集,然后从数组中获取一个随机键,然后从地图中获取该值
答案 1 :(得分:0)
Pair
)来存储数据(除非我对重复的假设是错误的)。ArrayList
从集合中提取随机值是一个完全独立的问题:
假设您希望列表中的随机数据子集使用Google Permutations
类。假设您想要列表中的随机下一个值(带有重复),请使用例如Math.random()*datalist.size()
并将其(向下)转换为整数以获取和索引元素,然后可以使用datalist.get(index)
提取。