Day8 Collections类

Collections类

  1. Collections类是针对集合操作的工具类

    Collections类的常用方法://public static <T extends Comparable<? super T>> void sort (List<T> list):将指定的列表按升序排列

    //public static void reverse (List<?> list):反转指定列表中元素的顺序 //public static void shuffle(List<?> list): 使用默认端随机源随机排列指定的列表。

public class CollectionsDemo{
    public static void main(String[] args){
        //创建ArrayList集合对象
        ArrayList<Student> array = new ArrayList<Student>();
        //创建学生对象
        Student s1 = new Student("令狐冲",20);
        Student s2 = new Student("岳不群",34);
        Student s3 = new Student("左冷禅",40);
        Student s4 = new Student("任我行",50);
        //使用Collections对ArrayList集合排序
        Collections.sort(array,new Comparator<Student>(){
            @Override
            public int compare(Student s1, Student s2){
                //按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
                int num = s1.getAge() - s2.getAge();
                int num2 = num ==0? s1.getName().compareTo(s2.getName()):num;
                return num2;
            }
        });
        //遍历集合
        for(Student s : array){
            System.out.println(s.getName() + "," + s.getAge());
        }
    }
}

//模拟斗地主小游戏

import java.util.*;
​
public class PokerDemo {
    public static void main(String[] args) {
        //创建HashMap,键是编号,值是牌
        HashMap<Integer,String> hm = new HashMap<Integer,String>();
        //创建ArrayList,存储编号
        ArrayList<Integer> array = new ArrayList<Integer>();
        //创建花色数组和点数数组
        String[] colors = {"♦","♣","♥","♠"};
        String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        //从0开始往HashMap里面存储编号,并存储对应的牌。同时往ArrayList里面存储编号
        int index = 0;
        for (String number : numbers){
            for (String color : colors){
                hm.put(index,color+number);
                array.add(index);
                index++;
            }
        }
        hm.put(index,"小王");
        array.add(index);
        index++;
        hm.put(index,"大王");
        array.add(index);
        //洗牌(洗的是编号),用Collections的shuffle()方法实现
        Collections.shuffle(array);
        //发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合接收)
        TreeSet<Integer> lhcSet = new TreeSet<Integer>();
        TreeSet<Integer> ybqSet = new TreeSet<Integer>();
        TreeSet<Integer> zlcSet = new TreeSet<Integer>();
        TreeSet<Integer> dpSet = new TreeSet<Integer>();
        for (int i = 0; i< array.size();i++){
            int x = array.get(i);
            if (i >= array.size() - 3){
                dpSet.add(x);
            }else if (i%3 == 0){
                lhcSet.add(x);
            }else if (i%3 == 1){
                ybqSet.add(x);
            }else if (i%3 == 2){
                zlcSet.add(x);
            }
        }
        //调用看牌方法
        lookPoker("令狐冲",lhcSet,hm);
        lookPoker("岳不群",ybqSet,hm);
        lookPoker("左冷禅",zlcSet,hm);
        lookPoker("底牌",dpSet,hm);
    }
​
    //定义方法看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
    public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer,String> hm){
        System.out.print(name + "的牌是:");
        for (Integer key : ts){
            String poker = hm.get(key);
            System.out.print(poker + " ");
        }
        System.out.println();
    }
}
​
令狐冲的牌是:♣3 ♦4 ♥5 ♦7 ♥7 ♠8 ♦9 ♥9 ♠9 ♣J ♥Q ♦K ♦A ♥A ♠A ♦2 ♥2 
岳不群的牌是:♥3 ♠3 ♥4 ♠4 ♠7 ♦10 ♣10 ♥10 ♦J ♦Q ♣Q ♠Q ♣K ♠K ♣A ♣2 小王 
左冷禅的牌是:♦3 ♣4 ♦5 ♣5 ♠5 ♣6 ♥6 ♠6 ♣7 ♦8 ♣8 ♥8 ♣9 ♠10 ♥K ♠2 大王 
底牌的牌是:♦6 ♥J ♠J 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值