【问题标题】:How do I shuffle a deque?我如何洗牌双端队列?
【发布时间】:2013-10-16 00:35:57
【问题描述】:

这是我的代码:

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);
   }
}

我知道如何洗牌List,但我如何使用Collections 洗牌双端队列? 请用代码回复,因为我不完全习惯 Java 术语/理论,我还在学习。

【问题讨论】:

    标签: java shuffle deque


    【解决方案1】:

    您不能使用java.util.Collections 洗牌Deque;它仅适用于实现List 的集合。您可以编写自己的 shuffle 例程,也可以先创建一个 List,对其进行 shuffle,然后将结果复制到 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);
    }
    

    当然,编写自己的 shuffle 例程的问题在于 Deque 接口不提供移动元素的方法。

    【讨论】:

    • 谢谢!我会选择这个作为答案 :) 快速提问,阅读其他答案 我看到了这个:Note that Deque is only the Interface, to Shuffle use a class that also implements List then you can use Collections.shuffle(List) 这就是你在上面所做的吗?
    • @Ryan - 没错。如果您想要一个同时实现ListDeque 的具体集合类型,您可以使用LinkedList。请注意,基于数组的集合对随机元素的访问时间为 O(1),而 LinkedList 的访问时间为 O(n)。 (改组ArrayList 比改组LinkedList 更快。)但是,对于直接迭代或在开始和/或结束时的重复操作,LinkedList 与数组相比具有同样好的(有时甚至更好)性能-基于集合。
    • 一般来说“编写你自己的”并不是很重要,除非它绝对对性能至关重要。改组的规范答案可能是在 ArrayList 中进行(因为改组需要随机访问),然后将其复制回 LinkedList/双端队列。出于这个原因,Collections.shuffle() 对数组中的非随机访问列表进行混洗。
    【解决方案2】:

    Collections.shuffle(List) 只接受 List 所以为了洗牌 Deque 你需要使用 Deque 的 LinkedList 实现。 LinkedList 实现了 Deque 和 List 接口。见http://docs.oracle.com/javase/tutorial/collections/implementations/deque.html

    public static void main(String[] args) {
    
        LinkedList<Integer> list = new LinkedList<Integer>();
    
        // 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);
        int retval = list.getFirst();
        System.out.println("Retrieved Element is = " + retval);
    }
    

    【讨论】:

    • 谢谢,将查看您发布的链接。我已经知道如何用 List 来做,但是双端队列部分让我感到困惑。 +1 了。
    猜你喜欢
    • 2018-07-02
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2013-02-17
    • 2017-05-06
    • 2016-01-19
    • 2013-05-21
    • 2012-04-17
    相关资源
    最近更新 更多