Collections、

一、Collections

public class Collections extends Object

Collection是接口,Collections是类
只有静态方法--直接用类名调用--针对集合操作的工具类
image.png
操作:
image.png

二、案例一

image.png
image.png
排序操作:
不能用sort(array),需要在Student类中重写CompareTo()方法
Collection 中有内部类添加比较器的方法

static <T> void sort(List<T> list,Comarator<? super T> c)

image.png

三、案例二

image.png
image.png
录入牌:
定义String数组就行,不用ArrayList<>就复杂了
两个增强for
image.png
洗牌:

Collections.sort(array);

发牌:
这里必须把底牌的存入写到第一个if里,写后面就不执行了
image.png
看牌:
image.png

案例升级

image.png
image.png
image.png
HashMAp:
键是编号Integer,值是牌String,而不是ArrayList<>
image.png
录入牌:
嵌套增强for 外层是number,内层color,才能得到四种花色同时从小到大的顺序
不需要new ArrayLis存poker,直接存入HashMap里
image.png

洗牌:
洗编号
image.png

发牌:
TreeSet自动自然排序
image.png
看牌:
在main方法外自定义方法
需要有索引TreeSet 和 HashMap
image.png
调用方法:
image.png

评论

暂无评论