Collections.shuffle(列表列表)

发布于 2024-09-03 17:50:50 字数 91 浏览 7 评论 0原文

什么会促使人们使用这种方法?

更新:我现在明白了。我喜欢 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黎夕旧梦 2024-09-10 17:50:50

人们想要随机打乱有序元素序列的原因可能有很多。例如,一副纸牌。

洗牌不是一个简单的算法,就像排序一样 - 因此它很常见,需要一个库函数。

至于为什么是列表 - 显然它必须是一个有序的集合,因此不是任何通用的集合。仅保证列表及其子类型是有序的。 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.

黯然 2024-09-10 17:50:50

嗯,如果你有一个集合,并且你想要洗牌......

最明显的例子是纸牌游戏,其中你有代表单张纸牌的对象,以及代表你想要洗牌的牌组的集合。

另一个例子可能是,如果您在调查问卷中向用户提供多个答案,并且您不希望由于答案的顺序而出现任何偏差 - 因此您向每个用户提供一组经过整理的答案以供选择。

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.

网名女生简单气质 2024-09-10 17:50:50

好吧,想象一下您正在对一副纸牌进行建模。 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.

流星番茄 2024-09-10 17:50:50

如何使用此方法的一些想法:

  • 在游戏中洗牌
  • 在测试用例中随机化数组以实现排序算法
  • 在测试套件中洗牌测试用例以确保它们不相互依赖
  • 如果您尝试解决 NP -像旅行推销员这样的完整问题,一种方法是获取输入,将其打乱几次,然后使用长度最短的结果。这为您提供了一个在 O(N) 时间内运行的解决方案(其中 N 是节点数)。

Some ideas how you could use this method:

  • Shuffle cards in a game
  • Randomize an array in a test case for a sorting algorithm
  • Shuffling test cases in your test suite to make sure they don't depend on each other
  • If you try to solve an NP-complete problem like the traveling salesman, one approach is to take the input, shuffle it several times and then use the result with the shortest length. This gives you a solution which runs in O(N) time (where N is the number of nodes).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文