Java playing cards (Shuffle Collections.shuffle)

There is a static shuffle() method under the Java.util.Collections class, as follows:

1) static void shuffle(List<?> list) uses the default random source to permutate the list, and the probability of all permutations is roughly equal.

2) static void shuffle(List<?> list, Random rand) Use the specified random source to replace the specified list. The probability of all replacements is roughly equal. It is assumed that the random source is fair.

In layman's terms, it's like shuffling cards, randomly disrupting the original order.

Note: If an integer array is given, use the Arrays.asList() method to convert it into a collection class, there are two ways:

1) Use List<Integer> list=ArrayList(Arrays.asList(ia)), shuffle() will not change the order of the underlying array.

2) Using List<Integer> list=Arrays.aslist(ia), then shuffle() will change the order of the underlying array. The code example is as follows:
 

package com.item.test;
import java.util.*;

public class T1 {
    public static void main(String[] args){
        Random rand=new Random(54);
        Integer[] ia={0,1,2,3,4,5,6,7,8,9};
        List<Integer> list=new ArrayList<Integer>(Arrays.asList(ia));
        System.out.println("前 shufflig: "+list);
        Collections.shuffle(list,rand);
        System.out.println("后 shuffling: "+list);
        System.out.println("数组: "+Arrays.toString(ia));
        List<Integer> list1=Arrays.asList(ia);
        System.out.println("前 shuffling: "+list1);
        Collections.shuffle(list1,rand);
        System.out.println("后 shuffling: "+list1);
        System.out.println("数组: "+Arrays.toString(ia));
    }
}

effect:

So continue an article: [Java Poker (enum)]

package com.item.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class Action {
    public static void main(String[] args) {
        //随机卡牌
        List<Card> list=ResetCard(StartCard());
        int i=0;
        for (Card c:list) {
            if(i%3==0){
                System.out.println();
            }
            System.out.print(c.getIndex()+":"+c.getName()+"\t");
            i++;
        }
    }

    /**
     * 随机排序
     * @param list
     * @return
     */
    public static List<Card> ResetCard(List<Card> list){
        Collections.shuffle(list);
        return list;
    }

    /**
     * 初始化卡牌
     * @return
     */
    public static List<Card> StartCard(){
        List<Card> list=new ArrayList<>(54);
        int i=0;
        for (CardsEnum cardsEnum:CardsEnum.values()) {
            for (CardColorEnum cardColorEnum:CardColorEnum.values()) {
                list.add(new Card(cardsEnum,cardColorEnum,i++));
            }
        }
        Card big=new Card();
        big.setName(CardBossEnum.BIG.getName());
        big.setIndex(i++);
        Card small=new Card();
        small.setName(CardBossEnum.SMALL.getName());
        small.setIndex(i++);
        list.add(big);
        list.add(small);
        return list;
    }
}

The effect is as follows:

 

Last article address: [ https://blog.csdn.net/feng8403000/article/details/114792614 ]

Guess you like

Origin blog.csdn.net/feng8403000/article/details/114793908