使用 Java 打乱单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4247810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:30:06  来源:igfitidea点击:

Scramble a Word using Java

javashuffle

提问by Mark Peters

I wanted to scramble a String, to make it unreadable and so came up with this method:

我想打乱一个字符串,使其不可读,所以想出了这个方法:

public String scrambleWord(String start_word){

     char[] wordarray = start_word.toCharArray();

        char[] dummywordarray = start_word.toCharArray();

        Random random = new Random();

        int r = random.nextInt(wordarray.length-1);
        int i = 0;

        int j = r+1;

        while(i <= r){

            dummywordarray[wordarray.length -i-1] = wordarray[i];

            i++;
        }


        while (j <= wordarray.length -1){

            dummywordarray[j-r-1] = wordarray[j];

            j++;

        }

        String newword = String.valueOf(dummywa);



        return newword;

SO I first converted the string to a char array, and in my method I had to duplicate the char array "dummywordarray". Passing once through this algorithm every lette rof the word will have changed position. But it wont be scrambled very well, in the sense that you could put it back together at a glance. SO I passed a given String of less than 9 characters through the method 7 times, and the words are fairly well scrambled, i.e. unreadable. But I tried it with a 30 character string and it took 500 passes before I could guarantee it was nicely scrambled. 500! I'm sure there is a better algorithm, I'd like some advice on either a)improving this method or b)a better way.

所以我首先将字符串转换为字符数组,在我的方法中,我必须复制字符数组“dummywordarray”。通过该算法一次,单词的每个字母都会改变位置。但它不会被很好地打乱,从某种意义上说,你一眼就可以把它放回原处。因此,我通过该方法 7 次传递了少于 9 个字符的给定字符串,并且这些单词相当混乱,即不可读。但是我用 30 个字符的字符串尝试了它,在我保证它被很好地打乱之前,它花了 500 遍。500!我确定有更好的算法,我想就 a) 改进此方法或 b) 更好的方法提供一些建议。

回答by Mark Peters

How about

怎么样

ArrayList<Character> chars = new ArrayList<Character>(word.length());
for ( char c : word.toCharArray() ) {
   chars.add(c);
}
Collections.shuffle(chars);
char[] shuffled = new char[chars.size()];
for ( int i = 0; i < shuffled.length; i++ ) {
   shuffled[i] = chars.get(i);
}
String shuffledWord = new String(shuffled);

In other words, you could take advantage of the existing java.util.Collections.shuffle(List)method. Unfortunately you have to jump through a couple of hoops to use it, since you can't use primitives in Generics.

换句话说,您可以利用现有的java.util.Collections.shuffle(List)方法。不幸的是,您必须跳过几个环节才能使用它,因为您不能在泛型中使用原语。

Edit:

编辑:

The basic way that shuffleworks (see the Javadoc for the full explanation), is like this:

工作的基本方式shuffle(请参阅 Javadoc 以获取完整说明),如下所示:

for position = last_index to first_index
   let swap_pos = random number between first_index and position, inclusive
   swap(swap_pos, position)

Edit 2:

编辑2:

This approach is significantly less verbose with Guava's Charsutilities:

这种方法对于 Guava 的Chars实用程序来说明显不那么冗长:

List<Character> chars = Chars.asList(word.toCharArray());
Collections.shuffle(chars);
String shuffledWord = new String(Chars.toArray(chars));