What will prompt one to use this method?
Update : I see the point now. I like the reason of Uri "Shuffling is not a trivial algorithm". That is quite true.
There can be many reasons one would want to randomly shuffle an ordered sequence of elements. For instance, a deck of cards.
Shuffling is not a trivial algorithm, just as sorting isn't - So it is common enough to necessitate a library function.
As to why a list - obviously it has to be an ordered collection, thus not any general Collection. Only list and its subtypes are guaranteed to be ordered. The Collections class does not provide operations for arrays, but you could (and probably should, for performance) pass an ArrayList to this method.
Um, if you have a collection, and you want to shuffle it...
The most obvious example would be a card game, where you have objects representing individual cards, and a collection representing the deck which you want to shuffle.
Another example might be if you're presenting the user with multiple answers in a questionnaire, and you don't want there to be any bias due to the ordering of the answers - so you present each user a shuffled set of answers to pick from.
Well, imagine you're modeling a deck of cards. Shuffle would be one of the first functions you'd write.
Anytime you'd want to randomize the contents of a collection, you'd use shuffle.
Some ideas how you could use this method:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With