P162-集合框架-Collections工具类的使用+扑克牌发牌案例

 

 

 

 

package cd_one.code16.exer2;

import org.junit.Test;

import java.util.*;

public class CollectionTest {
    @Test
    public void test(){
    
        List list = Arrays.asList(45, 43, 65, 6, 43, 2, 32, 45, 56, 34, 23);
        System.out.println(list);
        //[45, 43, 65, 6, 43, 2, 32, 45, 56, 34, 23]:
        Collections.reverse(list);
        System.out.println(list);
        //[23, 34, 56, 45, 32, 2, 43, 6, 65, 43, 45]:反转
        Collections.shuffle(list);
        System.out.println(list);
        //[43, 43, 45, 65, 34, 6, 23, 2, 56, 45, 32]随机排序

        Collections.sort(list, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Integer && o2 instanceof Integer){
                    Integer i1 = (Integer) o1;
                    Integer i2 = (Integer) o2;

                    return (i1.intValue() - i2.intValue());
                }
                throw new RuntimeException("类型不匹配");
            }
        });
        System.out.println(list);
        //[2, 6, 23, 32, 34, 43, 43, 45, 45, 56, 65]
//        [65, 56, 45, 45, 43, 43, 34, 32, 23, 6, 2]
    }
    @Test
    public void test2(){
        List list = Arrays.asList(45, 43, 65, 6, 43, 2, 32, 45, 56, 34, 23);
        System.out.println(list);
        Object max = Collections.max(list);

        Object max1 = Collections.max(list, new Comparator() {

            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Integer && o2 instanceof Integer){
                    Integer i1 = (Integer) o1;
                    Integer i2 = (Integer) o2;

                    return -(i1.intValue() - i2.intValue());
                }
                throw new RuntimeException("类型不匹配");
            }
        });

        System.out.println(max);//65
        System.out.println(max1);//2

        int count = Collections.frequency(list,45);
        System.out.println("45出现了"+ count +"次");
    }
    @Test
    public void test3(){
        List src = Arrays.asList(45, 43, 65, 6, 43, 2, 32, 45, 56, 34, 23);
        List dest = Arrays.asList(new Object[src.size()]);

        Collections.copy(dest,src);

        System.out.println(dest);//[45, 43, 65, 6, 43, 2, 32, 45, 56, 34, 23]
    }
    @Test
    public void test4(){
        List list1 = new ArrayList();
        list1.add(34);
        list1.add(12);
        list1.add(45);
        List list2 = Collections.unmodifiableList(list1);
        System.out.println(list2.get(0));//34
    }
    @Test
    public void test5(){
        List list1 = new ArrayList<>();
        List list2 = Collections.synchronizedList(list1);
        list2.add(123);

        HashMap map1 = new HashMap<>();
        Map map2 = Collections.synchronizedMap(map1);
    }
}

排序

package cd_one.code16.exer2;

import java.util.ArrayList;
import java.util.Collections;

public class PokerTest {
    public static void main(String[] args) {
        //1.组成一幅扑克牌
        String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        String[] color = {"方片◆","梅花▲","红桃●","黑桃★"};
        ArrayList poker = new ArrayList<>();

        for (int i = 0; i < color.length; i++) {
            for (int j = 0; j < num.length; j++) {
                poker.add(color[i] + " " + num[j]);
            }
        }
        //添加大小王
        poker.add("小王");
        poker.add("大王");

        //2.洗牌
        Collections.shuffle(poker);
        System.out.println(poker);
        System.out.println();

        //3.发牌
        //3.1创建3个角色和1个底牌对应的4个ArrayList
        ArrayList tom = new ArrayList<>();
        ArrayList jerry = new ArrayList<>();
        ArrayList me = new ArrayList<>();
        ArrayList lastCards = new ArrayList<>();

        for (int i = 0; i < poker.size(); i++) {
            if(i >= poker.size() - 3){
                lastCards.add(poker.get(i));
            }
            if(i % 3 == 0){
                tom.add(poker.get(i));
            } else if (i % 3 == 1) {
                jerry.add(poker.get(i));
            } else if (i % 3 == 2) {
                me.add(poker.get(i));
            }
        }
//        3.2遍历显示4个ArrayList
        System.out.print("Tom:");
        System.out.print(tom+"\n");
        System.out.println();
        System.out.print("Jerry:");
        System.out.print(jerry+"\n");
        System.out.println();
        System.out.print("Me:");
        System.out.print(me+"\n");
        System.out.println();
        System.out.print("LastCards:");
        System.out.print(lastCards);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃炫迈的绮绮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值