Java实现随机返回字符串数组中的字符串

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;
    }

猜你喜欢

转载自blog.csdn.net/huacode/article/details/80673244