要随机返回一个Set的里的元素, 如何操作呢?# Java - 爪哇娇娃
c*n
1 楼
比如有Set set
现在要在set里随机取一个元素出来, 如何取呢?
目前只想到一个比较笨的办法:
int i = (int) (random() * set.toArray().length);
return ( set.toArray() )[i];
这个方法有没有什么问题? 有更好的方法么?
现在要在set里随机取一个元素出来, 如何取呢?
目前只想到一个比较笨的办法:
int i = (int) (random() * set.toArray().length);
return ( set.toArray() )[i];
这个方法有没有什么问题? 有更好的方法么?