一、JDK9的新特性*
- JDK9的新特性:
List接口,Set接口,Map接口:里边增加一个静态的方法of,可以给集合一次性添加多个元素
static < E > list< E > of (E …elements)- 使用前提:
当集合中存储的元素的个数已经确定了,不再改变时使用- 注意:
- of方法只适用于List接口,Set接口,Map接口,不适用于接接口的实现类
- of方法的返回值是一个不能改变的集合,集合不能再使用add,put方法添加元素,会抛出异常
- Set接口和Map接口在调用of方法的时候,不能有重复的元素,否则会抛出异常
package day01.Demo03;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Demo03JDK9 {
public static void main(String[] args) {
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);//[a, b, c, d]
//set.add("w"); //UnsupportedOperationException不支持操作的异常
//Map<String, Integer> map = Map.of("张三", 18, "李四", 19, "王五", 20,"张三",18);//IllegalArgumentException:非法参数异常,有重复的元素
Map<String, Integer> map = Map.of("张三", 18, "李四", 19, "王五", 20);
System.out.println(map);//{王五=20, 李四=19, 张三=18}
//map.put("w",10); //UnsupportedOperationException不支持操作的异常
}
}
运行结果:
二、Debug追踪
- Debug调试程序:
可以让代码逐行执行,查看代码执行的过程,调试程序中出现的bug- 使用方式:
在行号的右边,鼠标左键单击,添加断点(每个方法的第一行,哪里有bug添加到哪里)
右键,选择Debug执行程序
程序就会停留在添加的第一个断点处- 执行程序:
f8:逐行执行程序
f7:进入到方法中
shift+f8:跳出方法
f9:跳到下一个断点,如果没有下一个断点,那么就结束程序
ctrl+f2:退出debug模式,停止程序
Console:切换到控制台
三、斗地主案例的需求分析(双列)
斗地主综合案例:有序版本
1. 准备牌
2. 洗牌
3. 发牌
4. 看牌
package day01.Demo03;
import java.util.*;
public class Demo03DouDiZhu {
public static void main(String[] args) {
//1.准备牌
//创建一个Map集合,存储牌的索引和组装好的牌
HashMap<Integer, String> poker = new HashMap<>();
//创建一个List集合,存储牌的索引
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++;
//循环嵌套遍历两个集合,组装52张牌,存储到集合中
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);
/*
2.洗牌
使用Collections中的方法shuffle(list)
*/
Collections.shuffle(pokerIndex);
//System.out.println(pokerIndex);
/*
3.发牌
*/
//定义4个集合,存储玩家牌的索引,和底牌的索引
ArrayList<Integer> player01 = new ArrayList<>();
ArrayList<Integer> player02 = new ArrayList<>();
ArrayList<Integer> player03 = new ArrayList<>();
ArrayList<Integer> dipai = new ArrayList<>();
//遍历存储牌的索引list集合,获取每一个牌的索引
for (int i = 0; i < pokerIndex.size() ; i++) {
Integer in = pokerIndex.get(i);
//先判断底牌
if(i>=51){
dipai.add(in);
}else if(i%3==0) {
//给玩家1发牌
player01.add(in);
}else if(i%3==1) {
//给玩家2发牌
player02.add(in);
}else if(i%3==2) {
//给玩家3发牌
player03.add(in);
}
}
/*
4.排序
使用Collections中的方法sort(list)
*/
Collections.sort(player01);
Collections.sort(player02);
Collections.sort(player03);
Collections.sort(dipai);
/*
5.看牌
调用看牌的方法
*/
lookPoker("刘德华",poker,player01);
lookPoker("周润发",poker,player02);
lookPoker("周星驰",poker,player03);
lookPoker("底牌",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) {
//使用牌的索引,去Map集合中,找到对应的牌
String value = poker.get(key);
System.out.print(value+" ");
}
System.out.println();//打印完每一个玩家的牌,换行
}
}
运行结果: