简体   繁体   中英

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 :

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.

you should first implement your cardList before being able to shuffle it

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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