Sev,
You're right, my solution does alter the original array. I hadn't realised that. But it's trivial to change:
This makes good use of library methods, but is probably a constant-factor slower than other methods that don't involve copying the array. Also, it won't work with basic types - you have to change the cast. A suitably intelligent array-copier could fix this I guess.
On a side note, I think my solution runs in linear time...I'm assuming Collections.shuffle() is linear. Your solution two is gonna run in quadratic time. I'd probably use an ArrayList, and keep track of the maximum 'legal' element - similar to Junilu's shuffle algorithm. Solution 1 runs in worst-case quadratic time (if you randomly generated index 0 every time) but the average time may well be better...I haven't analysed it thoroughly enough
Anyway, that is not so important. I know efficiency isn't a huge concern here, but all algorithms are of the same simplicity, so choosing the most efficient one is reasonable.
--Tim
[ June 22, 2004: Message edited by: Tim West ]
[ June 22, 2004: Message edited by: Tim West ]