簡體   English   中英

混淆了Java鏈表問題的值

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

我試圖使用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);

您正在整理一個空列表。

嘗試將shuffle()行移到方法的末尾。

您應該先實現您的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