import java.util.Arrays;
public class Test {
public static void main(String[] args) {
// 特点:
test(); //不传数据
test(10); // 传输一个数据
test(10,20,30); // 传输多个数据
test(new int[]{10,20,30,40}); // 传输一个数组
}
//注意事项1:一个形参列表中,只能有一个可变参数
//注意事项2:可变参数必须放在形参列表的最后面
public static void test(int...nums){
// 可变参数在方法内部,本质就是一个数组
System.out.println(nums.length);
System.out.println(Arrays.toString(nums));
System.out.println("-------------------------");
}
}
方法名称 | 说明 |
---|---|
public static <T> boolean addAll(Collection<? super T> c,T...elements) | 给集合批量添加元素 |
public static void shuffle(List<?> list) | 打乱List集合中的元素顺序 |
public static <T>?void sort(List<?> list) | 对List集合中的元素进行升序排序 |
public static <T>?void?sort(List<?> list,Comparator<? super T> c) | 对List集合中的元素,按照比较器对象指定的规则进行排序 |
Card类
public class Card {
private String number; //数值
private String color; //花色
// 每张牌是存在大小的
private int size; //0 1 2……
public Card() {
}
public Card(String number, String color, int size) {
this.number = number;
this.color = color;
this.size = size;
}
@Override
public String toString() {
return color + number;
}
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;
}
}
Room类
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Room {
private List<Card> allCards = new ArrayList<>();
public Room(){
// 做出54张牌,存入到集合中
// 点数:个数确定,类型确定
String[] numbers = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
// 花色:个数确定,类型确定
String[] colors = {"?","?","?","?"};
int size = 0; //表示每张牌的的大小
// 遍历点数,再遍历花色,组织牌
for (String number : numbers) {
size++;
for (String color : colors) {
// 得到一张牌
Card c = new Card(number,color,size);
allCards.add(c); //存入牌
}
}
// 单独存小王和大王
Card c1 = new Card("","?",++size);
Card c2 = new Card("","?",++size);
Collections.addAll(allCards,c1,c2);
}
// 游戏启动
public void start() {
//1.洗牌
Collections.shuffle(allCards);
//2.发牌
List<Card> user1 = new ArrayList<>();
List<Card> user2 = new ArrayList<>();
List<Card> user3 = new ArrayList<>();
for (int i = 0; i < allCards.size() - 3; i++) {
Card c = allCards.get(i);
if (i % 3 == 0){
user1.add(c);
}else if (i % 3 == 1){
user2.add(c);
}else if (i % 3 == 2){
user3.add(c);
}
}
// 对三个玩家的牌进行排序
sortCards(user1);
sortCards(user2);
sortCards(user3);
// 看牌
System.out.println("一号玩家:" + user1);
System.out.println("----------------------------------------------------------");
System.out.println("二号玩家:" + user2);
System.out.println("----------------------------------------------------------");
System.out.println("三号玩家:" + user3);
System.out.println("----------------------------------------------------------");
List<Card> lastThreeCards = allCards.subList(allCards.size() - 3,allCards.size());
System.out.println("底牌:" + lastThreeCards);
}
// 集中进行排序
private void sortCards(List<Card> cards) {
Collections.sort(cards, new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
return o2.getSize() - o1.getSize(); //降序排序
}
});
}
}
Test类
public class Test {
public static void main(String[] args) {
Room room = new Room();
room.start();
}
}