- package com.bjsxt.sort.util.collections;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- public class CollectionsDemo01{
-
- public static void main(String[] args) {
-
- List<Integer> cards = new ArrayList<Integer>();
-
- //shuffle 洗牌 ,模拟斗地主
-
- for(int i=0;i<54;i++){
-
- cards.add(i);
- }
-
- //打乱集合牌的顺序
-
- Collections.shuffle(cards);
-
- //3个人的手牌
-
- List<Integer> person1 = new ArrayList<Integer>();
-
- List<Integer> person2 = new ArrayList<Integer>();
-
- List<Integer> person3 = new ArrayList<Integer>();
-
- //底牌
-
- List<Integer> last = new ArrayList<Integer>();
-
- //依次发牌
-
- for(int i=0;i<51;i+=3){
-
- person1.add(cards.get(i));
-
- person2.add(cards.get(i+1));
-
- person3.add(cards.get(i+2));
-
- }
-
- //最后三张为底牌
-
- last.add(cards.get(51));
-
- last.add(cards.get(52));
-
- last.add(cards.get(53));
-
- System.out.println("第一个人的手牌:"+person1);
-
- System.out.println("第二个人的手牌:"+person2);
-
- System.out.println("第三个人的手牌:"+person3);
-
- System.out.println("底牌为:"+last);
-
- }
- }
复制代码 运行是这样的:
- 第一个人的手牌:[53, 51, 41, 21, 14, 29, 2, 19, 25, 46, 36, 40, 23, 17, 16, 32, 22]
- 第二个人的手牌:[50, 37, 15, 12, 0, 35, 3, 9, 49, 8, 20, 44, 26, 34, 48, 5, 38]
- 第三个人的手牌:[18, 10, 6, 24, 45, 43, 7, 11, 30, 47, 33, 28, 27, 52, 13, 1, 42]
- 底牌为:[4, 31, 39]
复制代码
|
|