Is there a way to apply Knuth shuffle to a stack data structure?

For the programming class, I am creating a blackjack program for the first homework assignment. The professor gave us a sample of the Card class, which includes a method of adding them to the deck. For her deck, she uses an ArrayList, which you can easily use Knuth Shuffle using the Collections.shuffle () method.

This method does not work for Stacks though (obviously), but I think that the Stack structure will work best for this program, because you can pop up and insert cards into and out of the deck.

+4
source share
8 answers

Both java.util.ArrayList<E> and java.util.stack<E> implement java.util.List<E> , and Collections.shuffle() takes java.util.List<?> As a parameter. You should be able to pass Stack to Collections.shuffle() if you are not using another stack implementation that does not implement java.util.List<E> . If so, I would advise you to switch to another implementation of the stack.

+18
source

I think it is much easier to perform stack operations in an ArrayList.

+2
source

The stack is a list, so you can call Collections.shuffle () on the stack.

However, Stack is an old class, such as Vector and a deprecated kind. You should currently use Dequeue (a double queue that works both on the queue and on the stack), not on the stack , but Dequeues are not lists, so they cannot be shuffled.

In addition, you can always put your cards in a list, shuffle them, and then add all of them to Dequeue

+1
source

There is no reason why the stack structure should also not be random access (java.util.Stack does, although this has its problems). Other than that, you can put stack elements in an ArrayList, shuffle, and then reinsert them on your stack.

0
source

No, the Fischer-Yate move depends on random access to the dataset. You need a collection that 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 a specific stack class. Use what you have, do not invent new classes.

0
source

Adam responds best to the stack. For card games, what I usually use is a simple arraist and removes random elements. No shuffling required.

0
source

Just shuffle before / when you put the cards on the stack. Since a correctly executed Knuth shuffle does not allow you to replace cards in that part of the already walked deck, you can simply put them on the stack when you go ...

Since java will not allow you to treat the stack as a random access list, simply copy from the stack to an ArrayList to execute the shuffling phase (the optional 52 ArrayList, which is confusing, doesn't really matter)

0
source

The Collections.shuffle () method does this for you, you do not need to explicitly specify.

"If the specified list does not implement the RandomAccess interface and is large, this shuffle () implementation removes the specified list into the array before moving it and unloads the shuffled array back into the list. This avoids quadratic behavior that would result from shuffling the" sequential access "list.

this is what the java documentation says about the implementation of the Collections.shuffle () method, so passing the java.util.Stack (implementation of the java.util.List interface) should work ...

0
source

All Articles