Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to apply the Knuth shuffle to a Stack data structure?

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.

like image 316
Logan Serman Avatar asked Nov 29 '22 12:11

Logan Serman


1 Answers

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.

like image 164
Adam Rosenfield Avatar answered Dec 05 '22 10:12

Adam Rosenfield