Collections.shuffle(列表列表)
什么会促使人们使用这种方法?
更新:我现在明白了。我喜欢 Uri 的理由“洗牌不是一个简单的算法”。这是千真万确的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
人们想要随机打乱有序元素序列的原因可能有很多。例如,一副纸牌。
洗牌不是一个简单的算法,就像排序一样 - 因此它很常见,需要一个库函数。
至于为什么是列表 - 显然它必须是一个有序的集合,因此不是任何通用的集合。仅保证列表及其子类型是有序的。 Collections 类不提供数组操作,但您可以(并且可能应该,为了性能)将 ArrayList 传递给此方法。
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.
好吧,想象一下您正在对一副纸牌进行建模。 Shuffle 将是您首先编写的函数之一。
任何时候你想要随机化集合的内容,你都可以使用随机播放。
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: