String[] strings = new String[]{"壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖","拾"};
LinkedList<String> linkedList=new LinkedList();
linkedList.addAll(Arrays.asList(strings));
int n=linkedList.size();//长度n
Random random=new Random();
StringBuilder sb=new StringBuilder();
for (int i = 0; i < 4; i++) {//取4个字符
int r = random.nextInt(n);//取0-n的长度(不包括n)
String tmp=linkedList.remove(r);//移除链表元素
sb.append(tmp);//字符拼接
n--;//n减1
}
Java 在一串字符中取 4个不重复的字符
最新推荐文章于 2024-08-04 11:07:52 发布