JAVA实现随机获取字符串数组中的任意一个字符串
//随机返回字符串数组中的字符串
public static String RandomStr(String[] strs){
int random_index = (int) (Math.random()*strs.length);
return strs[random_index];
}
JAVA实现随机不重复获取字符串数组中的任意一个字符串
//随机不重复获取一个字符串数组中的字符串,flag_1为字符串数组的长度
public static String RandomFile(){
int random_index = (int) (Math.random()*flg_1);
String str = null;
str = FileName[random_index];
FileName[random_index] = FileName[flg_1 - 1];
flg_1 --;
return str;
}

本文介绍两种JAVA实现方式:一种是从字符串数组中随机选取一个字符串;另一种是随机且不重复地从字符串数组中选取一个字符串。这两种方法都通过生成随机索引来实现目标。
2889

被折叠的 条评论
为什么被折叠?



