简体   繁体   English

混淆了Java链表问题的值

[英]mix up the values of Java linked List issue

i'm trying to mix up the values of my linked list using Collections.shuffle but i always get the same order every time this is the code who supposed to display and mix up my list of Card : 我试图使用Collections.shuffle来混合我的链表的值,但是每次这是应该显示并混合我的Card列表的代码时,我总是得到相同的顺序:

public Deck(int nbBox) {
    this.cardList = new LinkedList<Card>();
    Collections.shuffle(cardList);
    for (int i = 0; i < nbBox; i++) {
      for (Color col : Color.values()) {
        for (Value val : Value.values()) {
          cardList.add(new Card(val, col));
        }
      }

    }

  }
this.cardList = new LinkedList<Card>();
    Collections.shuffle(cardList);

you were shuffling an empty list. 您正在整理一个空列表。

try to move the shuffle() line to the end of your method. 尝试将shuffle()行移到方法的末尾。

you should first implement your cardList before being able to shuffle it 您应该先实现您的cardList,然后才能对其进行洗牌

public Deck(int nbBox) {
    this.cardList = new LinkedList<Card>();
    for (int i = 0; i < nbBox; i++) {
      for (Color col : Color.values()) {
        for (Value val : Value.values()) {
          cardList.add(new Card(val, col));
        }
      }   
    }
  Collections.shuffle(cardList);     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM