{ "question_id": 384519, "question_title": "生成随机字符串", "question_content": "生成一个由大写字母和数字组成的6位随机字符串,并且字符串不重复", "difficulty": "简单", "answer_id": 420998, "answer_content": "public static char[] generate() {\n\t\t\n\t\tchar[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',\n\t\t\t\t'K', 'L', 'M', 'N', 'O', 'P','Q', 'R', 'S', 'T', 'U', 'V',\n\t\t\t\t'W', 'X', 'Y', 'Z','0','1','2','3','4','5','6','7','8','9'};\n\t\tboolean[] flags = new boolean[letters.length];\n\t\tchar[] chs = new char[6];\n\t\tfor (int i = 0; i < chs.length; i++) {\n\t\t\tint index;\n\t\t\tdo {\n\t\t\t\tindex = (int) (Math.random() * (letters.length));\n\t\t\t} while (flags[index]);// 判断生成的字符是否重复\n\t\t\tchs[i] = letters[index];\n\t\t\tflags[index] = true;\n\t\t}\n\t\treturn chs;\n\t}", "tag_name": "java", "java": "class java_384519 {\n\tpublic static char[] generate() {\n\tchar[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',\n\t\t'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };\n\tboolean[] flags = new boolean[letters.length];\n\tchar[] chs = new char[6];\n\tfor (int i = 0; i < chs.length; i++) {\n\t int index;\n\t do {\n\t\tindex = (int) (Math.random() * (letters.length));\n\t } while (flags[index]);\n\t chs[i] = letters[index];\n\t flags[index] = true;\n\t}\n\treturn chs;\n }\n}", "topic_link": "https://bbs.csdn.net/topics/600470188", "status": 1, "keywords": "图算法,算法高阶,最小生成树,最小生成树的形成", "license": "csdn.net", "notebook": { "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/11.ipynb?type=file" }, "notebook_enable": 1 }