可变参数与Collections工具类

一、可变参数

可变参数与Collections工具类_第1张图片

import java.util.Arrays;

public class ParameterTest {
    public static void main(String[] args) {
        //特点
        //1.不传数据
        test (  );
        //2.一个数据
        test ( 1 );
        //3.多个数据
        test ( 1,2,3 );
        //4.传入数组
        test ( new int[]{1,2,3,4,5,6} );
    }
    //注意事项1:一个形参列表中,只能有一个可变参数
    //注意事项2:可变参数必须放在形参列表的最后面
    public static void test(int ...numbers){
        //可变参数在方法内部就是一个数组  numbers是数组
        int length = numbers.length;
        System.out.println ( length );
        System.out.println ( Arrays.toString ( numbers ) );
    }
}

 二、Collections集合工具类

可变参数与Collections工具类_第2张图片

 可变参数与Collections工具类_第3张图片

 


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectionTest {
    public static void main(String[] args) {
        //1.public static  boolean addAll(Collection c,T...elements):
        //为集合批量添加元素
        List name=new ArrayList<> ();
        Collections.addAll ( name,"张三","李四","王五","周六","砸吧" );
        System.out.println ( name );//[张三, 李四, 王五, 周六, 砸吧]
        //2.public static void shuffle(List list):打乱list集合元素中的顺序
        Collections.shuffle ( name );
        System.out.println ( name );//[王五, 砸吧, 李四, 张三, 周六] 打乱顺序
        //3.public static  void sort(List list):对list中的元素进行升序排序
        Listlist=new ArrayList<> ();
        Collections.addAll ( list,1,6,8,9,5,4 );
        System.out.println ( list );//[1, 6, 8, 9, 5, 4]
        Collections.sort ( list );
        System.out.println ( list );//[1, 4, 5, 6, 8, 9]
        //4.public static  void sort(List list,Comparator c)
        //对list集合中的元素 按照比较器对象制定的规则进行排序
        List students=new ArrayList<> ();
        Student s1 = new Student ("猪猪侠",25,169.5);
        Student s2 = new Student ("菲菲公主",22,166.5);
        Student s3 = new Student ("猪猪侠",25,169.5);
        students.add ( s1 );
        students.add ( s2 );
        students.add ( s3 );
        Collections.sort ( students, new Comparator () {
            @Override
            public int compare(Student o1, Student o2) {
                return  Integer.compare ( o1.age,o2.age );
            }
        } );
        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}]


    }
}

三、案例(斗地主游戏)


public class Card {
    private  String numbers; //牌的点数
    private  String color;//牌的花色
    //每张牌还存在大小
    private  int size;

    @Override
    public String toString() {
        return color+numbers;
    }

    public Card(String[] number, String[] color, int size) {
    }

    public Card(String numbers, String color, int size) {
        this.numbers = numbers;
        this.color = color;
        this.size = size;
    }

    public String getNumbers() {
        return numbers;
    }

    public void setNumbers(String numbers) {
        this.numbers = numbers;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Room {
    //房间必须有一副牌
    private List allCards=new ArrayList<> ();
    public  Room(){
        //1.做出54张牌,存入到akkCards中
        //a.点数,个数确定 ,类型确定
        //a.点数
        String [] number={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
        //b.花色
        String[] color={"♥","♠","♣","♦"};
        int size=0;//表示每张牌的大小
        //c.遍历点数 遍历花色 组织牌
        for (String numbers : number) {
            size++;//1,2....
            for (String colors : color) {
                Card c=new Card (numbers,colors,size);
                allCards.add ( c );
            }
        }
        //单独存入大小王
        Card x =new Card ( "小王","",++size);
        Card X=new Card ( "大王","",++size);
        Collections.addAll ( allCards,x,X );
        System.out.println ("新牌"+allCards);
    }

    /**
     * 游戏启动
     */
    public void start() {
        //1.洗牌
        Collections.shuffle ( allCards );
        System.out.println ("洗牌后"+ allCards );
        //2.发牌  定义三个玩家 List(ArrayList)
        List farmer1 =new ArrayList<> ();
        List farmer2 =new ArrayList<> ();
        List dizhu =new ArrayList<> ();
        //3.发牌给三个玩家,依次发出51张牌  留下3张作为底牌(轮循--求余)
        for (int i = 0; i < allCards.size ()-3; i++) {//留三张牌
            Card c=allCards.get ( i );
            if (i%3==0){
                //发给农民1
                farmer1.add ( c );
            }else if (i%3==1){
                //发给农名2
                farmer2.add ( c );
            }else if (i%3==2){
                //发给地主
                dizhu.add ( c );
            }
        }
        //4.对三个玩家的牌进行排序
        sortCards(farmer1);
        sortCards ( farmer2 );
        sortCards ( dizhu );

//        Collections.sort ( farmer1, new Comparator () {
//            @Override
//            public int compare(Card o1, Card o2) {
//                return Integer.compare ( o1.getSize (),o2.getSize () );
//            }
//        } );
//        Collections.sort ( farmer2, new Comparator () {
//            @Override
//            public int compare(Card o1, Card o2) {
//                return Integer.compare ( o1.getSize (),o2.getSize () );
//            }
//        } );
//        Collections.sort ( dizhu, new Comparator () {
//            @Override
//            public int compare(Card o1, Card o2) {
//                return Integer.compare ( o1.getSize (),o2.getSize () );
//            }
//        } );
        //5.看牌
        System.out.println ( farmer1 );
        System.out.println ( farmer2 );
        System.out.println ( dizhu );
        //存储三张底牌
        List lastThreeCards = allCards.subList ( allCards.size () - 3, allCards.size () );
        sortCards ( lastThreeCards );
        System.out.println ( lastThreeCards );


    }

    private void sortCards(List  list) {
        Collections.sort ( list, new Comparator () {
            @Override
            public int compare(Card o1, Card o2) {
                return Integer.compare ( o1.getSize (),o2.getSize () );
            }
        } );
    }
}
public class GameDemo {

    public static void main(String[] args) {
        //1.牌类
        //2.房间
        Room m=new Room ();
        //3.启动游戏
        m.start();
    }
}

你可能感兴趣的:(java,算法,开发语言)