有没有办法将 Knuth shuffle 应用于 Stack 数据结构?
对于编程课,我正在为第一个家庭作业创建一个二十一点程序。 教授给了我们一个示例 Card 类,其中包括将它们添加到牌组中的方法。 对于她的牌组,她使用 ArrayList,您可以使用 Collections.shuffle() 方法轻松地进行 Knuth Shuffle。
虽然该方法不适用于堆栈(显然),但我认为堆栈结构最适合此程序,因为您可以将卡片弹出或推入牌组或从牌组中推出。
For a programming class I am creating a blackjack program for the first homework assignment. The professor has given us a sample Card class, which includes the method to add them to a deck. For her deck, she uses an ArrayList, which you can easily Knuth Shuffle with the Collections.shuffle() method.
That method does not work for Stacks though (obviously), but I think a Stack structure would work best for this program because you may pop and push cards into and out of the deck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
两者
java.util.ArrayList< /code>
和
java.lang. util.stack
实现java.util.List
接口和Collections.shuffle()
接受一个java.util.List ;
作为参数。 您应该能够将Stack
传递到Collections.shuffle()
中,除非您使用的不同堆栈实现未实现java.util.list< ;E>
。 如果是的话,我建议您切换到不同的堆栈实现。Both
java.util.ArrayList<E>
andjava.util.stack<E>
implement thejava.util.List<E>
interface, andCollections.shuffle()
takes ajava.util.List<?>
as a parameter. You should be able to pass aStack
intoCollections.shuffle()
, unless you're using a different stack implementation that does not implementjava.util.list<E>
. If you are, I would advise you to switch to a different stack implementation.我想在 ArrayList 上进行堆栈操作要容易得多。
I guess it's much easier to do stack operations on an ArrayList.
堆栈是一个列表,因此您可以在堆栈上调用 Collections.shuffle() 。
也就是说,Stack 是一个古老的类,就像 Vector 一样,有点过时了。 现在你会使用 Dequeue (双端队列,既可以作为队列也可以作为堆栈,而不是堆栈但是,出队不是列表,因此不能对它们进行洗牌。
此外,您始终可以将卡片放入列表中,将它们洗牌,然后将它们全部添加到出队中
A stack is a list, so you can call Collections.shuffle() on your stack.
That said, Stack is an old class, like Vector and kind of outmoded. Nowadays you would use a Dequeue (a double ended queue which works as either a queue or a stack) rather then a stack but, Dequeues are not lists, so they can't be shuffled.
Also, you can always put your cards in a List, shuffle them, and then add all of them to a Dequeue
堆栈结构没有理由不应该是随机访问的(java.util.Stack 是随机访问的,尽管它有它自己的问题)。 除此之外,您可以将堆栈的元素弹出到 ArrayList 中,进行随机播放,然后将它们推回到堆栈中。
There is no reason why a stack structure should not be random access as well (java.util.Stack does, although that has problems of its own). Other than that, you can pop the elements of the stack into an ArrayList, shuffle and then push them back on to your stack.
不,Fisher-Yates 洗牌依赖于对数据集的随机访问。 您需要一些允许 get(int index) 的 Collection。 如果您需要堆栈,只需使用列表即可。 推送和弹出只需调用 get(0) 和 add(0) 即可。 这比实现一些自定义堆栈类更好。 使用你所拥有的,不要发明新的类。
No, Fisher-Yates shuffle relies on random access to the dataset. You need some Collection which allows get(int index). If you need a stack just use a list. push and pop just call get(0) and add(0). This is better than implementing some custom stack class. Use what you have, don't invent new classes.
Adam 的答案最适合堆栈。 对于卡牌游戏,我通常使用的是一个简单的数组列表并删除随机元素。 无需洗牌。
Adam's answer is best for a stack. For card games, what I usually use is a simple arraylist and remove random elements. No shuffling required.
只需在将牌放入牌堆之前/同时洗牌即可。
由于正确实现的 Knuth 洗牌不允许替换已遍历的牌组部分中的牌,因此您可以简单地将它们放入堆栈中......
因为 java 不会让您将堆栈视为随机访问列表从堆栈复制到 ArrayList 中以进行洗牌阶段(额外的 52 个元素 ArrayList 四处乱转没什么大不了的)
Just shuffle before/as you put the cards onto the stack.
Since a properly implemented Knuth shuffle does not allow replacement of cards in the part of the deck already traversed you can simply place them onto the stack as you go along...
Since java will not let you treat a stack as a random access list just copy from the stack into an ArrayList to do the shuffling phase (an extra 52 element ArrayList knocking around is no big deal)
Collections.shuffle() 方法可以为您执行此操作,而无需显式执行。
“如果指定的列表未实现 RandomAccess 接口并且很大,则 shuffle() 的此实现会在对指定列表进行混洗之前将其转储到数组中,并将混洗后的数组转储回列表中。这避免了可能导致的二次行为避免重新排列“顺序访问”列表。”
这是java文档中关于Collections.shuffle()方法实现的说法
所以传递 java.util.Stack (java.util.List 接口的实现)应该可以...
the Collections.shuffle() method does that for you you dont have to explicitly.
"If the specified list does not implement the RandomAccess interface and is large, this implementation of shuffle() dumps the specified list into an array before shuffling it, and dumps the shuffled array back into the list. This avoids the quadratic behavior that would result from shuffling a "sequential access" list in place."
this is what the java documentation says about Collections.shuffle() method implementation
so passing a java.util.Stack (an implementation of java.util.List interface) should work...