!204 修复 RandomUti 中数组越界异常

Merge pull request !204 from yichengxian/hutool-ycx
This commit is contained in:
Looly 2020-10-29 18:29:08 +08:00 committed by Gitee
commit 80dd13773a

View File

@ -310,6 +310,9 @@ public class RandomUtil {
* @return 随机元素
*/
public static <T> T randomEle(List<T> list, int limit) {
if (list.size() < limit){
limit = list.size();
}
return list.get(randomInt(limit));
}
@ -335,6 +338,9 @@ public class RandomUtil {
* @since 3.3.0
*/
public static <T> T randomEle(T[] array, int limit) {
if (array.length < limit){
limit = array.length;
}
return array[randomInt(limit)];
}
@ -606,4 +612,5 @@ public class RandomUtil {
return DateUtil.offset(baseDate, dateField, randomInt(min, max));
}
}