问题描述
对于一个编程类,我正在为第一个作业分配创建一个二十一点程序。教授给了我们一个样本卡类,其中包括将它们添加到甲板上的方法。对于她的甲板,她使用一个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.
该方法对于Stacks来说不起作用(显然) ,但是我认为一个Stack结构对于这个程序来说最有效,因为你可以弹出和推出卡片进出卡片。
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.
推荐答案
p> 和实现界面,将一个 java.util.List<?>
作为参数呃您应该可以将 Stack
传递到 Collections.shuffle()
中,除非您使用不同的堆栈实现这不实现 java.util.list< E>
。如果你是,我建议你切换到不同的堆栈实现。
Both java.util.ArrayList<E>
and java.util.stack<E>
implement the java.util.List<E>
interface, and Collections.shuffle()
takes a java.util.List<?>
as a parameter. You should be able to pass a Stack
into Collections.shuffle()
, unless you're using a different stack implementation that does not implement java.util.list<E>
. If you are, I would advise you to switch to a different stack implementation.
这篇关于有没有办法将Knuth shuffle应用到Stack数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!