简体   繁体   English

使用Java拼写单词

[英]Scramble a Word using Java

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". 因此,我首先将字符串转换为char数组,在我的方法中,我必须复制char数组“ 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, ie unreadable. 因此,我通过该方法传递了少于9个字符的给定字符串7次,并且单词被很好地加扰,即不可读。 But I tried it with a 30 character string and it took 500 passes before I could guarantee it was nicely scrambled. 但是我用30个字符串尝试了一下,花了500遍才可以保证它被很好地打乱了。 500! 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. 我确定有更好的算法,我想对a)改进此方法或b)更好的方法提出一些建议。

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. 换句话说,您可以利用现有的java.util.Collections.shuffle(List)方法。 Unfortunately you have to jump through a couple of hoops to use it, since you can't use primitives in Generics. 不幸的是,由于不能在泛型中使用基元,因此必须跳过几个步骤才能使用它。

Edit: 编辑:

The basic way that shuffle works (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 Chars utilities: 使用Guava的Chars实用程序,此方法的冗长程度明显降低:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM