JDK9的新特性
List接口,Set接口,Map接口:里边增加了一个静态的方法of,可以给集合一次性添加多个元素
static <E> list<E> of (E... elements)
使用前提:
当集合中存储的元素的个数已经确定了,不再改变时使用
注意:
1. of方法只适用于List接口,Set接口,Map接口,不适用于接口的实现类
2. of方法的返回值时一个不能改变的集合,集合不能再使用add、put方法添加元素,会抛出异常
3. Set接口和Map接口在调用of方法的时候,不能有重复的元素,否则会抛出异常
List<String> list = List.of("a","b","a","c","d");
System.out.println(list); // [a, b, a, c, d]
// list.add("w"); // UnsupportedOperationException:不支持操作异常
// Set<String> set = Set.of("a", "b", "a", "c", "d");//IllegalArgumentException:非法参数异常,有重复元素
Set<String> set = Set.of("a", "b", "c", "d");
System.out.println(set);
//set.add("w");//UnsupportedOperationException:不支持操作异常
Debug
Debug调试程序:
可以让代码逐行执行,查看代码执行的过程,调试程序中出现的bug
使用方式:
在行号的右边,鼠标左键单击,添加断点(每个方法的第一行,哪里又有bug添加到哪里)
右键,选择Debug执行程序,程序就会停留在添加的第一个断点处
执行程序:
f8:逐行执行程序
f7:进入到方法中
shift + f8:跳出方法
f9:跳到下一个断点,如果没有下一个断点,那么就结束程序
ctrl + f2:退出debug模式,停止程序
Console:切换到控制台
斗地主综合案例:有序版本
1. 准备牌
特殊牌:大王、小王
52张牌:循环嵌套遍历两个集合/数组,组装52张牌
List<String> colors = List.of("♠","♥","♣","♦");
List<String> numbers = List.of("2","A","K"..."3");
Map<Integer, String> List<Integer> List<Integer>
牌的索引 组装好的牌 牌的索引 牌的索引
key value key key 【集合的索引】
0 大王 0 44 0
1 小王 1 shuffle(List) 22 1
2 ♠2 2 --> 0 2
3 ♠3 3 ... 3
4 ♠4 4 1 4
... ... ... 9 ...
53 ♦3 53 33 53
2. 洗牌
使用Collections中的方法shuffle(List)
3. 发牌
一人一张轮流发牌,每人17张 集合索引%3 剩余3张给底牌
4. 排序
使用Collections中的方法sort(List)
5. 看牌:可以使用查表法
遍历一个集合,获取另一个集合的key,通过key查找到value
遍历玩家和底牌的List集合,获取到Map集合的key,通过key找到value值
先判断底牌(i>=51)集合 底牌3张 sort(List)
(i%3==0) 集合 玩家1的牌17张 sort(List)
(i%3==1) 集合 玩家2的牌17张 sort(List)
(i%3==2) 集合 玩家3的牌17张 sort(List)
1. 准备牌
2. 洗牌
3. 发牌
4. 排序
5. 看牌
public static void main(String[] args) {
HashMap<Integer, String> poker = new HashMap<>();
ArrayList<Integer> pokerIndex = new ArrayList<>();
List<String> colors = List.of("♠", "♥", "♣", "♦");
List<String> numbers = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
int index = 0;
poker.put(index,"大王");
pokerIndex.add(index);
index++;
poker.put(index,"小王");
pokerIndex.add(index);
index++;
for (String number : numbers) {
for (String color : colors) {
poker.put(index,color+number);
pokerIndex.add(index);
index++;
}
}
// System.out.println(poker);
// System.out.println(pokerIndex);
Collections.shuffle(pokerIndex);
// System.out.println(pokerIndex);
ArrayList<Integer> player01 = new ArrayList<>();
ArrayList<Integer> player02 = new ArrayList<>();
ArrayList<Integer> player03 = new ArrayList<>();
ArrayList<Integer> diPai = new ArrayList<>();
for (int i = 0; i < pokerIndex.size(); i++) {
Integer in = pokerIndex.get(i);
if(i>=51) {
diPai.add(i);
} else if(i%3==0) {
player01.add(i);
} else if(i%3==1) {
player02.add(i);
} else if(i%3==2) {
player03.add(i);
}
}
Collections.sort(player01);
Collections.sort(player02);
Collections.sort(player03);
Collections.sort(diPai);
lookPoker("player01",poker,player01);
lookPoker("player02",poker,player02);
lookPoker("player03",poker,player03);
lookPoker("diPai",poker,diPai);
}
定义一个看牌的方法,提高代码的复用性
参数:
String name:玩家名称
HashMap<Integer, String> poker:存储牌的poker集合
ArrayList<Integer> list:存储玩家和底牌的list集合
查表法:
遍历玩家或者底牌集合,获取牌的索引
使用牌的索引,去Map集合中,找到对应的牌
public static void lookPoker(String name, HashMap<Integer, String> poker, ArrayList<Integer> list) {
System.out.print(name + ":");
for (Integer key : list) {
String value = poker.get(key);
System.out.println(value + " ");
}
System.out.println();
}