• 可变参数与Collections工具类


    一、可变参数

    1. import java.util.Arrays;
    2. public class ParameterTest {
    3. public static void main(String[] args) {
    4. //特点
    5. //1.不传数据
    6. test ( );
    7. //2.一个数据
    8. test ( 1 );
    9. //3.多个数据
    10. test ( 1,2,3 );
    11. //4.传入数组
    12. test ( new int[]{1,2,3,4,5,6} );
    13. }
    14. //注意事项1:一个形参列表中,只能有一个可变参数
    15. //注意事项2:可变参数必须放在形参列表的最后面
    16. public static void test(int ...numbers){
    17. //可变参数在方法内部就是一个数组 numbers是数组
    18. int length = numbers.length;
    19. System.out.println ( length );
    20. System.out.println ( Arrays.toString ( numbers ) );
    21. }
    22. }

     二、Collections集合工具类

     

     

    1. import java.util.ArrayList;
    2. import java.util.Collections;
    3. import java.util.Comparator;
    4. import java.util.List;
    5. public class CollectionTest {
    6. public static void main(String[] args) {
    7. //1.public static boolean addAll(Collection c,T...elements):
    8. //为集合批量添加元素
    9. List name=new ArrayList<> ();
    10. Collections.addAll ( name,"张三","李四","王五","周六","砸吧" );
    11. System.out.println ( name );//[张三, 李四, 王五, 周六, 砸吧]
    12. //2.public static void shuffle(List list):打乱list集合元素中的顺序
    13. Collections.shuffle ( name );
    14. System.out.println ( name );//[王五, 砸吧, 李四, 张三, 周六] 打乱顺序
    15. //3.public static void sort(List list):对list中的元素进行升序排序
    16. Listlist=new ArrayList<> ();
    17. Collections.addAll ( list,1,6,8,9,5,4 );
    18. System.out.println ( list );//[1, 6, 8, 9, 5, 4]
    19. Collections.sort ( list );
    20. System.out.println ( list );//[1, 4, 5, 6, 8, 9]
    21. //4.public static void sort(List list,Comparator c)
    22. //对list集合中的元素 按照比较器对象制定的规则进行排序
    23. List students=new ArrayList<> ();
    24. Student s1 = new Student ("猪猪侠",25,169.5);
    25. Student s2 = new Student ("菲菲公主",22,166.5);
    26. Student s3 = new Student ("猪猪侠",25,169.5);
    27. students.add ( s1 );
    28. students.add ( s2 );
    29. students.add ( s3 );
    30. Collections.sort ( students, new Comparator () {
    31. @Override
    32. public int compare(Student o1, Student o2) {
    33. return Integer.compare ( o1.age,o2.age );
    34. }
    35. } );
    36. System.out.println ( students );//[Student{name='菲菲公主', age=22, height=166.5}, Student{name='猪猪侠', age=25, height=169.5}, Student{name='猪猪侠', age=25, height=169.5}]
    37. }
    38. }

    三、案例(斗地主游戏)

    1. public class Card {
    2. private String numbers; //牌的点数
    3. private String color;//牌的花色
    4. //每张牌还存在大小
    5. private int size;
    6. @Override
    7. public String toString() {
    8. return color+numbers;
    9. }
    10. public Card(String[] number, String[] color, int size) {
    11. }
    12. public Card(String numbers, String color, int size) {
    13. this.numbers = numbers;
    14. this.color = color;
    15. this.size = size;
    16. }
    17. public String getNumbers() {
    18. return numbers;
    19. }
    20. public void setNumbers(String numbers) {
    21. this.numbers = numbers;
    22. }
    23. public String getColor() {
    24. return color;
    25. }
    26. public void setColor(String color) {
    27. this.color = color;
    28. }
    29. public int getSize() {
    30. return size;
    31. }
    32. public void setSize(int size) {
    33. this.size = size;
    34. }
    35. }
    1. import java.util.ArrayList;
    2. import java.util.Collections;
    3. import java.util.Comparator;
    4. import java.util.List;
    5. public class Room {
    6. //房间必须有一副牌
    7. private List allCards=new ArrayList<> ();
    8. public Room(){
    9. //1.做出54张牌,存入到akkCards中
    10. //a.点数,个数确定 ,类型确定
    11. //a.点数
    12. String [] number={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
    13. //b.花色
    14. String[] color={"♥","♠","♣","♦"};
    15. int size=0;//表示每张牌的大小
    16. //c.遍历点数 遍历花色 组织牌
    17. for (String numbers : number) {
    18. size++;//1,2....
    19. for (String colors : color) {
    20. Card c=new Card (numbers,colors,size);
    21. allCards.add ( c );
    22. }
    23. }
    24. //单独存入大小王
    25. Card x =new Card ( "小王","🃏",++size);
    26. Card X=new Card ( "大王","🃏",++size);
    27. Collections.addAll ( allCards,x,X );
    28. System.out.println ("新牌"+allCards);
    29. }
    30. /**
    31. * 游戏启动
    32. */
    33. public void start() {
    34. //1.洗牌
    35. Collections.shuffle ( allCards );
    36. System.out.println ("洗牌后"+ allCards );
    37. //2.发牌 定义三个玩家 List(ArrayList)
    38. List farmer1 =new ArrayList<> ();
    39. List farmer2 =new ArrayList<> ();
    40. List dizhu =new ArrayList<> ();
    41. //3.发牌给三个玩家,依次发出51张牌 留下3张作为底牌(轮循--求余)
    42. for (int i = 0; i < allCards.size ()-3; i++) {//留三张牌
    43. Card c=allCards.get ( i );
    44. if (i%3==0){
    45. //发给农民1
    46. farmer1.add ( c );
    47. }else if (i%3==1){
    48. //发给农名2
    49. farmer2.add ( c );
    50. }else if (i%3==2){
    51. //发给地主
    52. dizhu.add ( c );
    53. }
    54. }
    55. //4.对三个玩家的牌进行排序
    56. sortCards(farmer1);
    57. sortCards ( farmer2 );
    58. sortCards ( dizhu );
    59. // Collections.sort ( farmer1, new Comparator () {
    60. // @Override
    61. // public int compare(Card o1, Card o2) {
    62. // return Integer.compare ( o1.getSize (),o2.getSize () );
    63. // }
    64. // } );
    65. // Collections.sort ( farmer2, new Comparator () {
    66. // @Override
    67. // public int compare(Card o1, Card o2) {
    68. // return Integer.compare ( o1.getSize (),o2.getSize () );
    69. // }
    70. // } );
    71. // Collections.sort ( dizhu, new Comparator () {
    72. // @Override
    73. // public int compare(Card o1, Card o2) {
    74. // return Integer.compare ( o1.getSize (),o2.getSize () );
    75. // }
    76. // } );
    77. //5.看牌
    78. System.out.println ( farmer1 );
    79. System.out.println ( farmer2 );
    80. System.out.println ( dizhu );
    81. //存储三张底牌
    82. List lastThreeCards = allCards.subList ( allCards.size () - 3, allCards.size () );
    83. sortCards ( lastThreeCards );
    84. System.out.println ( lastThreeCards );
    85. }
    86. private void sortCards(List list) {
    87. Collections.sort ( list, new Comparator () {
    88. @Override
    89. public int compare(Card o1, Card o2) {
    90. return Integer.compare ( o1.getSize (),o2.getSize () );
    91. }
    92. } );
    93. }
    94. }
    1. public class GameDemo {
    2. public static void main(String[] args) {
    3. //1.牌类
    4. //2.房间
    5. Room m=new Room ();
    6. //3.启动游戏
    7. m.start();
    8. }
    9. }

  • 相关阅读:
    动态规划--完全背包问题详解2
    Spring读书笔记——bean创建(下)
    RabbitMQ的工作队列和交换机类型的概念与作用
    vue3的开发小技巧
    周星驰亲自下场招人 Web3.0究竟是什么?
    el-table 懒加载自动展开节点
    CRM(Mapper层)详细代码
    1102、判断语句、while循环、for循环
    【面试经典150 | 区间】用最少数量的箭引爆气球
    vue antv g6 编辑器
  • 原文地址:https://blog.csdn.net/m0_64703222/article/details/132914792