1、Collections

    • Collections概述和使用
      • 针对集合操作的工具类
    • Collections类的常用方法

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

        1. /*
        2. ArrayList存储学生对象,使用Collections对ArrayList进行排序
        3. 要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
        4. */
        5. public class CollectionsDemo {
        6. public static void main(String[] args) {
        7. //创建对象
        8. ArrayList<Student> arr = new ArrayList<>();
        9. Student s1 = new Student("a", 18);
        10. Student s2 = new Student("b", 20);
        11. Student s3 = new Student("c", 19);
        12. Student s4 = new Student("d", 19);
        13. arr.add(s1);
        14. arr.add(s2);
        15. arr.add(s3);
        16. arr.add(s4);
        17. Collections.sort(arr, new Comparator<Student>() {
        18. @Override
        19. public int compare(Student o1, Student o2) {
        20. int num = o1.getAge() - o2.getAge();
        21. int num2 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;
        22. return num2;
        23. }
        24. });
        25. for (Student s : arr) {
        26. System.out.println(s.getName() + ", " + s.getAge());
        27. }
        28. }
        29. }
    • 模拟斗地主

      • image.png ```java / 通过程序实现斗地主过程中的洗牌,发牌和看牌 / public class PokerDemo { public static void main(String[] args) { //创建牌盒 ArrayList arrayList = new ArrayList<>();

        //创建牌 //牌有四种花色,每种花色都有2-A的牌,大小王 //创建花色数组和数字数组,进行拼接得到需要的牌 String[] colors = {“♥”, “♦”, “♠”, “♣”}; String[] numbers = {“2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”, “10”, “J”, “Q”, “K”, “A”}; for (String color : colors) {

        1. for (String number : numbers) {
        2. arrayList.add(color + number);
        3. }

        } arrayList.add(“大王”); arrayList.add(“小王”);

        //洗牌 //使用Collections的shuffle方法 Collections.shuffle(arrayList);

        //发牌 //创建三个人的牌盒,以及底牌的牌盒并把牌分发给四个牌盒 ArrayList arrayList1 = new ArrayList<>(); ArrayList arrayList2 = new ArrayList<>(); ArrayList arrayList3 = new ArrayList<>(); ArrayList dpArrayList = new ArrayList<>(); for (int i = 0; i < arrayList.size(); i++) {

        1. String poker = arrayList.get(i);
        2. //如果是最后三张牌则放入底牌
        3. if (i >= arrayList.size() - 3) {
        4. dpArrayList.add(poker);
        5. } else if (i % 3 == 0) {
        6. arrayList1.add(poker);
        7. } else if (i % 3 == 1) {
        8. arrayList2.add(poker);
        9. } else if (i % 3 == 2) {
        10. arrayList3.add(poker);
        11. }

        }

        System.out.println(“王小恒的牌是:” + arrayList1); System.out.println(“王中恒的牌是:” + arrayList2); System.out.println(“王大恒的牌是:” + arrayList3); System.out.println(“底牌是:” + dpArrayList);

    1. }

    }

    1. - **模拟斗地主升级版**
    2. - ![image.png](https://cdn.nlark.com/yuque/0/2022/png/26723396/1652147795444-f2c9d8d5-ed40-4ab5-b288-7a59a5066bf2.png#clientId=u139c8eb2-e3d8-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=322&id=u177ba762&margin=%5Bobject%20Object%5D&name=image.png&originHeight=643&originWidth=1219&originalType=binary&ratio=1&rotation=0&showTitle=false&size=171445&status=done&style=none&taskId=ue58ae32c-e8b5-4af8-a834-0e3d85d5657&title=&width=609.5)
    3. ```java
    4. /*
    5. 模拟斗地主升级版
    6. 通过程序实现斗地主过程中的洗牌,发牌和看牌
    7. 对牌进行排序
    8. */
    9. public class PokerDemoPlus {
    10. public static void main(String[] args) {
    11. //创建索引和牌之间的映射
    12. HashMap<Integer, String> hm = new HashMap<>();
    13. //创建牌
    14. StringBuilder sb = new StringBuilder();
    15. String[] colors = {"♥", "♦", "♠", "♣"};
    16. String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
    17. for (String number : numbers) {
    18. for (String color : colors) {
    19. sb.append(color + number);
    20. sb.append(" ");
    21. }
    22. }
    23. sb.append("大王 ");
    24. sb.append("小王");
    25. String poker = sb.toString();
    26. String[] pokers = poker.split(" "); //得到牌的一个String数组
    27. //新建牌盒,牌盒中存储索引值即map中的键
    28. ArrayList<Integer> arr = new ArrayList<>();
    29. //将索引与牌对应,并把索引存入牌盒
    30. for (int i = 0; i < pokers.length; i++) {
    31. hm.put(i, pokers[i]);
    32. arr.add(i);
    33. }
    34. //洗牌 将索引值打乱
    35. Collections.shuffle(arr);
    36. //发牌,需要创建牌盒
    37. TreeSet<Integer> ts1 = new TreeSet<>();
    38. TreeSet<Integer> ts2 = new TreeSet<>();
    39. TreeSet<Integer> ts3 = new TreeSet<>();
    40. TreeSet<Integer> dpTs = new TreeSet<>();
    41. for (int i = 0; i < arr.size(); i++) {
    42. int pokerIndex = arr.get(i);
    43. //最后三张牌放入底牌盒
    44. if (i >= arr.size() - 3) {
    45. dpTs.add(pokerIndex);
    46. } else if (i % 3 == 0) {
    47. ts1.add(pokerIndex);
    48. } else if (i % 3 == 1) {
    49. ts2.add(pokerIndex);
    50. } else if (i % 3 == 2) {
    51. ts3.add(pokerIndex);
    52. }
    53. }
    54. lookPoker("王小恒", ts1, hm);
    55. lookPoker("王中恒", ts2, hm);
    56. lookPoker("王大恒", ts3, hm);
    57. lookPoker("底牌", dpTs, hm);
    58. System.out.println("");
    59. }
    60. public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) {
    61. System.out.print(name + "的牌是:");
    62. for (int index : ts) {
    63. System.out.print(hm.get(index) + " ");
    64. }
    65. System.out.println();
    66. }
    67. }