Java 在置乱字符串时防止重复字母?
我正在创建一个程序,它将输出字符串的字母,但会被置乱。它是有效的,但是没有办法防止一封信出现不止一次。我该怎么办 我的代码:Java 在置乱字符串时防止重复字母?,java,string,Java,String,我正在创建一个程序,它将输出字符串的字母,但会被置乱。它是有效的,但是没有办法防止一封信出现不止一次。我该怎么办 我的代码: import java.util.Random; public class Final { public static void main(String[] args) { Random rand = new Random(); String str = "pumpkinpie"; String[] spli
import java.util.Random;
public class Final {
public static void main(String[] args) {
Random rand = new Random();
String str = "pumpkinpie";
String[] split = str.split("");
for(int i = 0; i < split.length; i++){
int randomLet = rand.nextInt(split.length);
System.out.print(split[randomLet]);
}
}
}
import java.util.Random;
公开课决赛{
公共静态void main(字符串[]args){
Random rand=新的Random();
String str=“pumpkinpie”;
String[]split=str.split(“”);
对于(int i=0;i
将字母添加到数组/集合
/列表
,仅当数组/集合
/列表
不包含字母时才打印:
Set<String>...
for(int i = 0; i < split.length; i++){
int randomLet = rand.nextInt(split.length);
if(set does not contain split[randomLet]){
System.out.print(split[randomLet]);
add split[randomLet] to set
}
}
设置。。。
对于(int i=0;i
将字母添加到数组/集合
/列表
,仅当数组/集合
/列表
不包含字母时才打印:
Set<String>...
for(int i = 0; i < split.length; i++){
int randomLet = rand.nextInt(split.length);
if(set does not contain split[randomLet]){
System.out.print(split[randomLet]);
add split[randomLet] to set
}
}
设置。。。
对于(int i=0;i
您可以将字符串中的每个字符添加到ArrayList中,并使用Collections.shuffle()方法将其洗牌,该方法还接受一个随机实例:
//1. initiate the string you want to shuffle
String originalString = "pumkinpie";
//2. initiate a List into which the characters will be added
List<Character> characterList= new ArrayList<Character>();
//3. add each character from the String to the List
for (char character : originalString.toCharArray()) {
characterList.add(character);
};
//4. shuffle using Collections.shuffle
Collections.shuffle(characterList, new Random());
//5. print each character
for (Character character : characterList) {
System.out.println(character);
}
randomize "add" - characters as 123
a - 1
ad - 13
add - 132
! back to drawing block
//1。启动要洗牌的字符串
字符串originalString=“pumkinpie”;
//2. 启动一个将添加字符的列表
List characterList=新建ArrayList();
//3. 将字符串中的每个字符添加到列表中
for(字符:originalString.toCharArray()){
字符列表。添加(字符);
};
//4. 使用集合进行洗牌。洗牌
洗牌(characterList,new Random());
//5. 打印每个字符
用于(字符:字符列表){
System.out.println(字符);
}
您可以将字符串中的每个字符添加到ArrayList中,并使用Collections.shuffle()方法将其洗牌,该方法还接受一个随机实例:
//1. initiate the string you want to shuffle
String originalString = "pumkinpie";
//2. initiate a List into which the characters will be added
List<Character> characterList= new ArrayList<Character>();
//3. add each character from the String to the List
for (char character : originalString.toCharArray()) {
characterList.add(character);
};
//4. shuffle using Collections.shuffle
Collections.shuffle(characterList, new Random());
//5. print each character
for (Character character : characterList) {
System.out.println(character);
}
randomize "add" - characters as 123
a - 1
ad - 13
add - 132
! back to drawing block
//1。启动要洗牌的字符串
字符串originalString=“pumkinpie”;
//2. 启动一个将添加字符的列表
List characterList=新建ArrayList();
//3. 将字符串中的每个字符添加到列表中
for(字符:originalString.toCharArray()){
字符列表。添加(字符);
};
//4. 使用集合进行洗牌。洗牌
洗牌(characterList,new Random());
//5. 打印每个字符
用于(字符:字符列表){
System.out.println(字符);
}
主要是概念性答案
如果您试图将字符串随机化,请认识到放置字母不能重复的逻辑过滤器实际上会降低熵,从而降低任何目的的安全性或基本随机性。此外,这将非常困难,因为如果检测到重复,则需要重新加扰
例如:
//1. initiate the string you want to shuffle
String originalString = "pumkinpie";
//2. initiate a List into which the characters will be added
List<Character> characterList= new ArrayList<Character>();
//3. add each character from the String to the List
for (char character : originalString.toCharArray()) {
characterList.add(character);
};
//4. shuffle using Collections.shuffle
Collections.shuffle(characterList, new Random());
//5. print each character
for (Character character : characterList) {
System.out.println(character);
}
randomize "add" - characters as 123
a - 1
ad - 13
add - 132
! back to drawing block
作为一个人类,我们知道只有一个非重复字符串可以产生,dad(字符顺序为312和213),但是系统会命中每个选项,直到命中其中一个,每次都会重新开始
add > add > dad > dad > dda > dda
123 > 132 > 213 > 312 > 321 > 231
或者像“对不起”这样的词:
共有120种排列,其中有8种不同的方式,其中第3个和第4个字母以34
或43
方式重复(rr
)(两种方式,结果字符串中有四个位置)。在120种可能性中,这占了48种排列方式,其中许多排列方式直到许多字符出现后才被发现
在具有更多重复字符的单词中(例如,comdo
,具有6个不同的o
,这涵盖了可能的720个字符中的576个排列),您的脚本会由于颠簸而变得非常低效
但是,如果需要,最好的办法是编写一个函数,将一个单词随机化,检查,然后再次随机化。手工操作(一次一个字符)对于简单的单词来说要慢一些,对于麻烦的单词来说,你的运气和其他任何事情一样好。让幸运女神指引你……主要是概念性的答案 如果您试图将字符串随机化,请认识到放置字母不能重复的逻辑过滤器实际上会降低熵,从而降低任何目的的安全性或基本随机性。此外,这将非常困难,因为如果检测到重复,则需要重新加扰 例如:
//1. initiate the string you want to shuffle
String originalString = "pumkinpie";
//2. initiate a List into which the characters will be added
List<Character> characterList= new ArrayList<Character>();
//3. add each character from the String to the List
for (char character : originalString.toCharArray()) {
characterList.add(character);
};
//4. shuffle using Collections.shuffle
Collections.shuffle(characterList, new Random());
//5. print each character
for (Character character : characterList) {
System.out.println(character);
}
randomize "add" - characters as 123
a - 1
ad - 13
add - 132
! back to drawing block
作为一个人类,我们知道只有一个非重复字符串可以产生,dad(字符顺序为312和213),但是系统会命中每个选项,直到命中其中一个,每次都会重新开始
add > add > dad > dad > dda > dda
123 > 132 > 213 > 312 > 321 > 231
或者像“对不起”这样的词:
共有120种排列,其中有8种不同的方式,其中第3个和第4个字母以34
或43
方式重复(rr
)(两种方式,结果字符串中有四个位置)。在120种可能性中,这占了48种排列方式,其中许多排列方式直到许多字符出现后才被发现
在具有更多重复字符的单词中(例如,comdo
,具有6个不同的o
,这涵盖了可能的720个字符中的576个排列),您的脚本会由于颠簸而变得非常低效
但是,如果需要,最好的办法是编写一个函数,将一个单词随机化,检查,然后再次随机化。手工操作(一次一个字符)会慢很多