[算法系列] 随机字符不重复(Java)

public static String generate(){
        char[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                'W', 'X', 'Y', 'Z'};
        boolean[] flags = new boolean[letters.length];
        char[] choise = new char[5];
        for (int i = 0; i < choise.length; i++){
            int index;
            do {
                index = (int) (Math.random()*letters.length);
            }while (flags[index]);
            choise[i] = letters[index];
            flags[index] = true;
        }
        return String.valueOf(choise);
    }

你可能感兴趣的:(算法,java,算法)