I have got a char array (size 12) that can look like this:
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l'}
And I would like to create (in the most efficient way) a String that would be the result of taking the characters from the array and ordering them ~randomly (let's use that word), for example:
“ahbejclfkdig”
I tried solutions with StringBuffer and random placing, but there was the problem of positions repeating. Also, I tried Collections.shuffle, but I don’t quite get this one working. I also looked at linear feedback shift register, but I don’t think is appropriate here. It is quite simple case, I will not be operating on large numbers, so memory allocation and speed should not raise any major issues.
The following code which use Collections.shuffle works:
You can use shuffle but change it for StringBuilder. I would wouldn't use StringBuffer unless you have to use old versions of Java.
prints
You may look up the Random class in the Java API. This will enable you to create random array indexes. So you can pick the characters from the array in random order.
I have tried the following piece of code... I guess you can use it in your scenario!