可变参数:
就是一种特殊形参,定义在方法,构造器的形参列表中,格式是:数据类型...参数名称
可变参数的好处:
灵活的接收数据
特点:可以不传数据给它,可以传一个数据或者多个数据给它,也可以传一个数组给它
可变参数的注意事项:
可变参数在方法内部就是一个数组
一个形参列表中的可变参数只有一个
可变参数必须在形参列表的最后面
java
public class test {
public static void main(String[] args) {
ttt();//可以不传数据
ttt(1);//可以传一个数据
ttt(1,2,3);//可以传多个数据
ttt(new int[]{1,2,3});//可以传一个数组
}
public static void ttt(int...nums)
{
//可变参数在方法内部是一个数组
System.out.println(Arrays.toString(nums));
}
//只能有一个可变参数,且必须在形参列表的最后
public static void ppp(int age,int...s)
{}
}
Collections类:
是一个用来操作集合的工具类
public static <T> boolean addAll(Collection<? super T> c, T... elements)
Adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array.
往集合中批量添加数据
public static void shuffle(List<?> list)
Randomly permutes the specified list using a default source of randomness.
打乱List集合中的元素顺序
void sort(List<T> list)
对List集合中的元素进行升序排序
public static <T> void sort(List<T> list, Comparator<? super T> c)
Sorts the specified list according to the order induced by the specified comparator.
对List集合中的元素,按照比较器对象指定的规则进行排序
java
public class test {
public static void main(String[] args) {
//1 addAll
ArrayList<String>list=new ArrayList<>();
Collections.addAll(list,"java1","java2","java3");
System.out.println(list);
//2:shuffle
Collections.shuffle(list);
System.out.println(list);//[java2, java3, java1]
//sort,默认升序
Collections.sort(list);
System.out.println(list);//[java1, java2, java3]
//sort排序自定义对象
ArrayList<Student>list1=new ArrayList<>();
list1.add(new Student("aa",10));
list1.add(new Student("bb",90));
list1.add(new Student("cc",22));
Collections.sort(list1, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge()-o2.getAge();//按照年龄升序
}
});
Collections.sort(list1,(o1,o2)->o1.getAge()-o2.getAge());
System.out.println(list1);
}
}public class test {
public static void main(String[] args) {
//1 addAll
ArrayList<String>list=new ArrayList<>();
Collections.addAll(list,"java1","java2","java3");
System.out.println(list);
//2:shuffle
Collections.shuffle(list);
System.out.println(list);//[java2, java3, java1]
//sort,默认升序
Collections.sort(list);
System.out.println(list);//[java1, java2, java3]
//sort排序自定义对象
ArrayList<Student>list1=new ArrayList<>();
list1.add(new Student("aa",10));
list1.add(new Student("bb",90));
list1.add(new Student("cc",22));
Collections.sort(list1, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge()-o2.getAge();//按照年龄升序
}
});
Collections.sort(list1,(o1,o2)->o1.getAge()-o2.getAge());
System.out.println(list1);
}
}
以下是一个简单的游戏:
牌类:
java
public class Card {
private String number;
private String color;
private int size;
public Card() {
}
public Card(String number, String color, int size) {
this.number = number;
this.color = color;
this.size = size;//牌的大小
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
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;
}
@Override
public String toString() {
return "Card{" +
"number='" + number + '\'' +
", color='" + color + '\'' +
", size=" + size +
'}';
}
}
操作牌类
java
public class Room {
private ArrayList<Card>list=new ArrayList<>();
public Room() {
String []numbers={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
int size=0;
String []colors={"方块","梅花","红桃","黑桃"};
for (String number : numbers) {
size++;
for (String color : colors) {
Card c=new Card(number,color,size);
list.add(c);
}
}
Card c1=new Card("","小王",size++);
Card c2=new Card("","大王",size);
Collections.addAll(list,c1,c2);
System.out.println(list);
}
public void randomCard()
{
System.out.println("开始洗牌");
Collections.shuffle(list);
//System.out.println(list);
}
public void devoteCard()
{
ArrayList<Card>list1=new ArrayList<>();
ArrayList<Card>list2=new ArrayList<>();
ArrayList<Card>list3=new ArrayList<>();
for (int i = 0; i < list.size()-3; i++) {//只发51张牌
Card c=list.get(i);
if(i%3==0)
{
list1.add(c);
}
else if(i%3==1)
{
list2.add(c);
}
else
{
list3.add(c);
}
}
sortCard(list1);
sortCard(list2);//对牌进行大小排序
sortCard(list3);
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
System.out.println("底牌是");
List<Card> LastCard= list.subList(list.size()-3,list.size());//包前不包后
System.out.println(LastCard);
}
private void sortCard(ArrayList<Card> list2) {
/*Collections.sort(list2, new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
return o1.getSize()-o2.getSize();
}
});
}*/
Collections.sort(list2, (o1, o2) -> o1.getSize() - o2.getSize());
}
}
test:
java
public class test {
public static void main(String[] args) {
Room r=new Room();
r.randomCard();
ArrayList<Card>[]lists=new ArrayList[3];
r.devoteCard();
}
}
结果:
[Card{number='3', color='方块', size=1}, Card{number='3', color='梅花', size=1}, Card{number='3', color='红桃', size=1}, Card{number='3', color='黑桃', size=1}, Card{number='4', color='方块', size=2}, Card{number='4', color='梅花', size=2}, Card{number='4', color='红桃', size=2}, Card{number='4', color='黑桃', size=2}, Card{number='5', color='方块', size=3}, Card{number='5', color='梅花', size=3}, Card{number='5', color='红桃', size=3}, Card{number='5', color='黑桃', size=3}, Card{number='6', color='方块', size=4}, Card{number='6', color='梅花', size=4}, Card{number='6', color='红桃', size=4}, Card{number='6', color='黑桃', size=4}, Card{number='7', color='方块', size=5}, Card{number='7', color='梅花', size=5}, Card{number='7', color='红桃', size=5}, Card{number='7', color='黑桃', size=5}, Card{number='8', color='方块', size=6}, Card{number='8', color='梅花', size=6}, Card{number='8', color='红桃', size=6}, Card{number='8', color='黑桃', size=6}, Card{number='9', color='方块', size=7}, Card{number='9', color='梅花', size=7}, Card{number='9', color='红桃', size=7}, Card{number='9', color='黑桃', size=7}, Card{number='10', color='方块', size=8}, Card{number='10', color='梅花', size=8}, Card{number='10', color='红桃', size=8}, Card{number='10', color='黑桃', size=8}, Card{number='J', color='方块', size=9}, Card{number='J', color='梅花', size=9}, Card{number='J', color='红桃', size=9}, Card{number='J', color='黑桃', size=9}, Card{number='Q', color='方块', size=10}, Card{number='Q', color='梅花', size=10}, Card{number='Q', color='红桃', size=10}, Card{number='Q', color='黑桃', size=10}, Card{number='K', color='方块', size=11}, Card{number='K', color='梅花', size=11}, Card{number='K', color='红桃', size=11}, Card{number='K', color='黑桃', size=11}, Card{number='A', color='方块', size=12}, Card{number='A', color='梅花', size=12}, Card{number='A', color='红桃', size=12}, Card{number='A', color='黑桃', size=12}, Card{number='2', color='方块', size=13}, Card{number='2', color='梅花', size=13}, Card{number='2', color='红桃', size=13}, Card{number='2', color='黑桃', size=13}, Card{number='', color='小王', size=13}, Card{number='', color='大王', size=14}]
开始洗牌
[Card{number='3', color='方块', size=1}, Card{number='3', color='黑桃', size=1}, Card{number='4', color='黑桃', size=2}, Card{number='4', color='梅花', size=2}, Card{number='4', color='方块', size=2}, Card{number='6', color='方块', size=4}, Card{number='8', color='梅花', size=6}, Card{number='8', color='黑桃', size=6}, Card{number='9', color='梅花', size=7}, Card{number='10', color='黑桃', size=8}, Card{number='10', color='方块', size=8}, Card{number='J', color='红桃', size=9}, Card{number='J', color='梅花', size=9}, Card{number='J', color='黑桃', size=9}, Card{number='K', color='红桃', size=11}, Card{number='A', color='黑桃', size=12}, Card{number='2', color='黑桃', size=13}]
[Card{number='3', color='红桃', size=1}, Card{number='6', color='黑桃', size=4}, Card{number='6', color='红桃', size=4}, Card{number='7', color='方块', size=5}, Card{number='7', color='红桃', size=5}, Card{number='7', color='黑桃', size=5}, Card{number='10', color='梅花', size=8}, Card{number='J', color='方块', size=9}, Card{number='Q', color='黑桃', size=10}, Card{number='K', color='方块', size=11}, Card{number='K', color='梅花', size=11}, Card{number='K', color='黑桃', size=11}, Card{number='A', color='红桃', size=12}, Card{number='2', color='梅花', size=13}, Card{number='2', color='红桃', size=13}, Card{number='', color='小王', size=13}, Card{number='', color='大王', size=14}]
[Card{number='3', color='梅花', size=1}, Card{number='4', color='红桃', size=2}, Card{number='5', color='红桃', size=3}, Card{number='5', color='梅花', size=3}, Card{number='5', color='方块', size=3}, Card{number='5', color='黑桃', size=3}, Card{number='7', color='梅花', size=5}, Card{number='8', color='红桃', size=6}, Card{number='8', color='方块', size=6}, Card{number='9', color='黑桃', size=7}, Card{number='9', color='方块', size=7}, Card{number='9', color='红桃', size=7}, Card{number='10', color='红桃', size=8}, Card{number='Q', color='红桃', size=10}, Card{number='Q', color='梅花', size=10}, Card{number='Q', color='方块', size=10}, Card{number='2', color='方块', size=13}]
底牌是
[Card{number='A', color='梅花', size=12}, Card{number='6', color='梅花', size=4}, Card{number='A', color='方块', size=12}]