假如需要定义一个方法求和,该方法可以灵活的完成如下需求:
/**
* 目标:初步理解可变参数
* */
public class MethodCollection {
public static void main(String[] args) {
sum(1);
sum(2, 3);
sum(1,23, 435);
sum(new int[] {10, 20, 30});
}
public static void sum(int...para){
//注意,可变数组在方法内部其实就是一个数组
System.out.println(para.length);
}
}
可变参数
可变参数的作用
可变参数的主意事项
Collection集合工具类
Collections常用的API
Collections排序相关API
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MethodCollections {
public static void main(String[] args) {
String name1 = "乔峰";
String name2 = "段誉";
String name3 = "虚竹";
List<String> lists = new ArrayList<>();
// lists.add(name1);
// lists.add(name2);
// lists.add(name3);
Collections.addAll(lists, name1, name2, name3);
System.out.println(lists);
Collections.shuffle(lists);
System.out.println(lists);
}
}
综合使用案例(斗地主)
package com.itheima.d11_Collections;
/*
模拟斗地主游戏
* */
import java.util.*;
import static java.util.Collections.addAll;
//创建牌对象
class Card{
private String size; //点数
private String color; //花色
private int index; //大小
public Card() {
}
public Card(String size, String color, int index) {
this.size = size;
this.color = color;
this.index = index;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
@Override
public String toString() {
return color+size;
}
}
public class Test {
//1.定义一个牌集合,存储54张牌
public static List<Card> cards = new ArrayList<>();
//2.做牌
static{
//1.定义点数
String[] sizes = new String[]{"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
//2.定义花色
String[] colors = new String[]{"♥", "♦", "♠", "♣"};
//3.点数,花色组合
int index = 0;
for (String size : sizes) {
index++;
for (String color : colors) {
Card c = new Card(size, color, index);
cards.add(c);
}
}
//4.增加大小王
cards.add(new Card("", "🃏", ++index)); //小王
cards.add(new Card("", "🃏", ++index)); //大王
System.out.println(cards);
}
public static void main(String[] args) {
//3.洗牌
Collections.shuffle(cards);
System.out.println(cards);
//4.定义三个游戏玩家
List<Card> list1 = new ArrayList<>();
List<Card> list2 = new ArrayList<>();
List<Card> list3 = new ArrayList<>();
//5.发牌
for (int i = 0; i < cards.size()-3; i++) {
//轮循
if(i % 3 == 0){
list1.add(cards.get(i));
}else if(i % 3 == 1){
list2.add(cards.get(i));
}else{
list3.add(cards.get(i));
}
}
//6.截取3张底牌
List<Card> list = cards.subList(cards.size()-3, cards.size());
System.out.println(list);
//7.随机一名玩家抢地主
Random r = new Random();
int i = r.nextInt(3);
if(i == 0){
//玩家1,抢地主
list1.addAll(list);
System.out.println("玩家1抢地主");
}else if(i == 1){
list2.addAll(list);
System.out.println("玩家2抢地主");
}else{
list3.addAll(list);
System.out.println("玩家3抢地主");
}
//8.给玩家的牌排序
cardSort(list1);
cardSort(list2);
cardSort(list3);
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
}
//定义方法给玩家的牌排序
public static void cardSort(List<Card> list){
Collections.sort(list, new Comparator<Card>() {
@Override
public int compare(Card o1, Card o2) {
return o1.getIndex() - o2.getIndex();
}
});
}
}