How do I shuffle a deque?

Ryan

This is my code:

import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.List;

public class ArrayDequeDemo {
   public static void main(String[] args) {

      // create an empty array deque with an initial capacity
      Deque<Integer> deque = new ArrayDeque<Integer>(8);

      // use add() method to add elements in the deque
      deque.add(15);
      deque.add(30);
      deque.add(20);
      deque.add(18);        

      // let us print all the elements available in deque
      for (Integer number : deque) {
         System.out.println("Number = " + number);
      }

      //Collections.shuffle((List<?>) deque);
      // getFirst() will retrieve element at first(head) position
      int retval = deque.getFirst();
      System.out.println("Retrieved Element is = " + retval);
   }
}

I know how to shuffle a List, but how do I use the Collections to shuffle a deque? Please reply with code as I am not totally used to Java terms/theory, I am still learning.

Ted Hopp

You cannot use java.util.Collections to shuffle a Deque; it only works on collections that implement List. You can write your own shuffle routine or you can first create a List, shuffle it, and then copy the results to a Deque:

public static void main(String[] args) {

  // create an empty array deque with an initial capacity
  List<Integer> list = new ArrayList<Integer>(8);

  // use add() method to add elements in the deque
  list.add(15);
  list.add(30);
  list.add(20);
  list.add(18);        

  // let us print all the elements available in deque
  for (Integer number : list) {
     System.out.println("Number = " + number);
  }

  Collections.shuffle(list);
  Deque<Integer> deque = new ArrayDeque<Integer>(list);
  // getFirst() will retrieve element at first(head) position
  int retval = deque.getFirst();
  System.out.println("Retrieved Element is = " + retval);
}

The problem with writing your own shuffle routine, of course, is that the Deque interface does not provide a means of moving elements around.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I find out if a value is in a deque?

From Java

How do I shuffle an array in Swift?

From Dev

How do I shuffle the order of an array in Jekyll?

From Dev

How do I shuffle a multidimensional list in Python

From Dev

How do I shuffle a list of dictionaries?

From Dev

How do I get index of element in std::deque

From Dev

How do I use two consecutive elements of a deque at once in a for loop?

From Dev

How do I shuffle and deal cards one at a time to players?

From Dev

How do I create a Vec from a range and shuffle it?

From Dev

how do I card shuffle a pandas series quickly

From Java

How can I shuffle an array?

From Dev

How do I shuffle multiple arrays using the same new indexes for each?

From Dev

How do I shuffle a Javascript Array ensuring each Index is in a new position in the new Array?

From Dev

How do I make shuffle playlist button and repeat button in android studio

From Dev

How can I shuffle bits efficiently?

From Dev

How can I shuffle the letters of a word?

From Dev

How can i shuffle RLMResults in swift?

From Dev

How can I shuffle the contents of a RealmResults object

From Dev

How to shuffle a string of symbols (i.e. $, %, ^)

From Dev

How can i shuffle RLMResults in swift?

From Dev

how do i shuffle an array of numbers using python. A number is only allowed to move a step from its origin position

From Dev

How do you do the Fisher-Yates shuffle in Lua

From Dev

How can I shuffle a result I get from a Postgresql?

From Dev

My shuffle function is stopping at 26 for unknown reasons. How can I get it to shuffle all the cards?

From Dev

My shuffle function is stopping at 26 for unknown reasons. How can I get it to shuffle all the cards?

From Dev

How can I shuffle the list with constraints(related to the index of each elements)

From Dev

How can I shuffle the list with constraints(related to the index of each elements)

From Dev

how to struct initializer and deque

From Dev

C++ How do iterators for non-contiguous containers like deque find the next element

Related Related

  1. 1

    How do I find out if a value is in a deque?

  2. 2

    How do I shuffle an array in Swift?

  3. 3

    How do I shuffle the order of an array in Jekyll?

  4. 4

    How do I shuffle a multidimensional list in Python

  5. 5

    How do I shuffle a list of dictionaries?

  6. 6

    How do I get index of element in std::deque

  7. 7

    How do I use two consecutive elements of a deque at once in a for loop?

  8. 8

    How do I shuffle and deal cards one at a time to players?

  9. 9

    How do I create a Vec from a range and shuffle it?

  10. 10

    how do I card shuffle a pandas series quickly

  11. 11

    How can I shuffle an array?

  12. 12

    How do I shuffle multiple arrays using the same new indexes for each?

  13. 13

    How do I shuffle a Javascript Array ensuring each Index is in a new position in the new Array?

  14. 14

    How do I make shuffle playlist button and repeat button in android studio

  15. 15

    How can I shuffle bits efficiently?

  16. 16

    How can I shuffle the letters of a word?

  17. 17

    How can i shuffle RLMResults in swift?

  18. 18

    How can I shuffle the contents of a RealmResults object

  19. 19

    How to shuffle a string of symbols (i.e. $, %, ^)

  20. 20

    How can i shuffle RLMResults in swift?

  21. 21

    how do i shuffle an array of numbers using python. A number is only allowed to move a step from its origin position

  22. 22

    How do you do the Fisher-Yates shuffle in Lua

  23. 23

    How can I shuffle a result I get from a Postgresql?

  24. 24

    My shuffle function is stopping at 26 for unknown reasons. How can I get it to shuffle all the cards?

  25. 25

    My shuffle function is stopping at 26 for unknown reasons. How can I get it to shuffle all the cards?

  26. 26

    How can I shuffle the list with constraints(related to the index of each elements)

  27. 27

    How can I shuffle the list with constraints(related to the index of each elements)

  28. 28

    how to struct initializer and deque

  29. 29

    C++ How do iterators for non-contiguous containers like deque find the next element

HotTag

Archive