- 6.1 Collections概述和使用
- 案例:ArrayList存储学生对象并排序
- 案例:模拟斗地主
- 案例:模拟斗地主升级版
- Collections类的概述:是针对集合 *** 作的工具类。
- Collections类的常用方法:
(1)public static> void sort(List list):将指定的列表按升序排序;
(2)public static void reverse(List> list):反转指定列表中元素的顺序;
(3)public static void shuffle(List> list):使用默认的随机源随机排列指定的列表。
public static void main(String[] args) { ArrayList案例:ArrayList存储学生对象并排序arrayList = new ArrayList<>(); arrayList.add("liubei"); arrayList.add("zhangfei"); arrayList.add("guanyu"); System.out.println(arrayList);//[liubei, zhangfei, guanyu] Collections.sort(arrayList); System.out.println(arrayList);//[guanyu, liubei, zhangfei] Collections.reverse(arrayList); System.out.println(arrayList);//[zhangfei, liubei, guanyu] Collections.shuffle(arrayList); System.out.println(arrayList);//随机排序,比如扑克牌洗牌可以用 }
- 需求:ArrayList存储学生对象,使用Collections对ArrayList进行排序。要求按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序。
- 思路:
(1)定义学生类;
(2)创建ArrayList集合;
(3)创建学生对象;
(4)把学生对象添加到集合;
(5)使用Collections对ArrayList集合排序;
(6)遍历集合。
public static void main(String[] args) { List案例:模拟斗地主arrayList = new ArrayList<>(); arrayList.add(new Student("liubei",13)); arrayList.add(new Student("guanyu",1)); arrayList.add(new Student("zhangfei",50)); arrayList.add(new Student("liushan",50)); for (Student s : arrayList){ System.out.println(s); } //使用方法:public static > void sort(List list) //该方法使用的是元素的自然排序,如果我们想要使用这个方法,Student类就要实现自然排序接口 //报错:要求传入类型为List ;而我们传入的类型为List //reason: no instance(s) of type variable(s) T exist so that Student conforms to Comparable super T> //原因:不存在变量T类型的实例,以便于学生符合Comparable super T>(Comparable super T>即自然排序接口) //Collections.sort(arrayList); //比较器排序 //使用方法:public static void sort(List list, Comparator super T> c) Collections.sort(arrayList, new Comparator () { @Override public int compare(Student s1, Student s2) { int num = s1.getAge()-s2.getAge(); return num==0 ? s1.getName().compareTo(s2.getName()) : num; } }); for (Student s : arrayList){ System.out.println(s); } }
- 需求:通过程序实现斗地主过程中的洗牌,发牌和看牌。
- 思路:
(1)创建一个牌盒,也就是定义一个集合对象,用ArrayList集合实现;
(2)往牌盒里面装牌;
(3)洗牌,也就是把牌打散,用Collections的shuffle()实现
(4)发牌,也就是遍历集合,给三个玩家发牌;
(5)看牌,也就是三个玩家分别遍历自己的牌。
public static void main(String[] args) { ArrayList案例:模拟斗地主升级版arrayList = new ArrayList<>(); String[] colors = {"♦", "♣", "♥", "♠"}; String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; for (String color : colors) { for (String number : numbers) { arrayList.add(color + number); } } arrayList.add("大王"); arrayList.add("小王"); Collections.shuffle(arrayList); ArrayList liubei = new ArrayList<>(); ArrayList guanyu = new ArrayList<>(); ArrayList zhangfei = new ArrayList<>(); ArrayList hand = new ArrayList<>(); for (int i = 0; i < arrayList.size(); i++) { if (i >= arrayList.size() - 3) { hand.add(arrayList.get(i)); } else if (i % 3 == 0) { liubei.add(arrayList.get(i)); } else if (i % 3 == 1) { guanyu.add(arrayList.get(i)); } else if (i % 3 == 2) { zhangfei.add(arrayList.get(i)); } } lookCards(liubei); lookCards(guanyu); lookCards(zhangfei); lookCards(hand); } public static void lookCards(ArrayList arrayList) { for (String s : arrayList) { System.out.print(s + " "); } System.out.println(); }
- 需求:通过程序实现斗地主过程中的洗牌,发牌和看牌。要求对牌进行排序。
- 分析:
- 思路:
(1)创建HashMap,键是编号,值是牌;
(2)创建ArrayList,存储编号;
(3)创建花色数组和点数数组;
(4)从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号;
(5)洗牌(洗的是编号),用Collections的shuffle()实现;
(6)发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合接收);
(7)定义方法看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌);
(8)调用方法看牌。
public static void main(String[] args) { HashMaphs = new HashMap<>(); ArrayList arrayList = new ArrayList<>(); String[] colors = {"♦", "♣", "♥", "♠"}; String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"}; int count = 0; for (String number : numbers) { for (String color : colors) { arrayList.add(count); hs.put(count, color + number); count++; } } hs.put(count++, "小王"); arrayList.add(52); hs.put(count, "大王"); arrayList.add(53); Collections.shuffle(arrayList); TreeSet player1 = new TreeSet(); TreeSet player2 = new TreeSet(); TreeSet player3 = new TreeSet(); TreeSet hand = new TreeSet(); for (int i = 0; i < arrayList.size(); i++) { if (i >= arrayList.size() - 3) { hand.add(arrayList.get(i)); } else if (i % 3 == 0) { player1.add(arrayList.get(i)); } else if (i % 3 == 1) { player2.add(arrayList.get(i)); } else if (i % 3 == 2) { player3.add(arrayList.get(i)); } } System.out.print("玩家1:"); lookCards(player1, hs); System.out.print("玩家2:"); lookCards(player2, hs); System.out.print("玩家3:"); lookCards(player3, hs); System.out.print("底牌:"); lookCards(hand,hs); } public static void lookCards(TreeSet ts, HashMap hm) { for (int i : ts){ System.out.print(hm.get(i) + " "); } System.out.println(); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)