文章目录
- 集合嵌套
- List嵌套List
- List嵌套Map
- Map嵌套Map
- Collections类
- 方法
- 排序 sort
- 乱序 shuffle
- 斗地主案例
- 需求
- 思路
- 代码
- 日志框架
- 介绍
- 优势
- 体系结构
- Logback
- 概述
- 快速入门
- 配置详解
集合嵌套
List嵌套List
public static void main(String[] args){
//一个年级有许多班级,每个班级有许多学生,学生有姓名
//定义班级
List<String> classes1List = new ArrayList<>();
classes1List.add("zhangsan");
classes1List.add("lisi");
classes1List.add("wangwu");
List<String> classes2List = new ArrayList<>();
classes2List.add("zhangsan2");
classes2List.add("lisi2");
classes2List.add("wangwu2");
List<String> classes3List = new ArrayList<>();
classes3List.add("zhangsan3");
classes3List.add("lisi3");
classes3List.add("wangwu3");
//定义年级
List<List<String>> gradeList = new ArrayList<>();
gradeList.add(classes1List);
gradeList.add(classes2List);
gradeList.add(classes3List);
//遍历gradeList
for (List<String> classes : gradeList) {
for (int i = 0; i < classes.size(); i++) {
System.out.println(classes.get(i));
}
System.out.println("--------------------");
}
}
List嵌套Map
public static void main(String[] args) {
//先定义班级
Map<String,String> classes1Map = new HashMap<>();
classes1Map.put("001","zhangsan");
classes1Map.put("002","lisi");
classes1Map.put("003","wangwu");
Map<String,String> classes2Map = new HashMap<>();
classes2Map.put("001","zhangsan2");
classes2Map.put("002","lisi2");
classes2Map.put("003","wangwu2");
Map<String,String> classes3Map = new HashMap<>();
classes3Map.put("001","zhangsan3");
classes3Map.put("002","lisi3");
classes3Map.put("003","wangwu3");
//定义年级
List<Map<String,String>> gradeList = new ArrayList<>();
gradeList.add(classes1Map);
gradeList.add(classes2Map);
gradeList.add(classes3Map);
//遍历
for (Map<String, String> classes : gradeList) {
for (Map.Entry<String, String> entry : classes.entrySet()) {
String id = entry.getKey();
String name = entry.getValue();
System.out.println(id + "--" + name);
}
System.out.println("------------------");
}
}
Map嵌套Map
public static void main(String[] args) {
//先定义班级
Map<String,String> classes1Map = new HashMap<>();
classes1Map.put("001","zhangsan");
classes1Map.put("002","lisi");
classes1Map.put("003","wangwu");
Map<String,String> classes2Map = new HashMap<>();
classes2Map.put("001","zhangsan2");
classes2Map.put("002","lisi2");
classes2Map.put("003","wangwu2");
Map<String,String> classes3Map = new HashMap<>();
classes3Map.put("001","zhangsan3");
classes3Map.put("002","lisi3");
classes3Map.put("003","wangwu3");
//再定义年级
Map<String,Map<String,String>> gradeMap = new HashMap<>();
gradeMap.put("第一个班级",classes1Map);
gradeMap.put("第二个班级",classes2Map);
gradeMap.put("第三个班级",classes3Map);
//遍历
for (String s : gradeMap.keySet()) {
System.out.println("--------------");
for (Map.Entry<String, String> entry : gradeMap.get(s).entrySet()) {
String id = entry.getKey();
String name = entry.getValue();
System.out.println(s + ":" + id + "--" + name);
}
}
}
Collections类
方法
排序 sort
乱序 shuffle
斗地主案例
需求
思路
- 准备牌
- 完成数字和纸牌的映射,使用Map集合
- 利用list集合记录牌的编号
- 洗牌:利用Collections.shuffle方法对其洗牌
- 发牌:将牌的编号对3取余,剩下3张作为底牌
- 利用TreeSet集合保存三个人的拍编号
- 看牌,通过TreeSet集合中牌的编号利用Map集合查看牌
代码
public static void main(String[] args) {
//准备牌
Map<Integer, String> pokerMap = new HashMap<>();
List<Integer> pokerNum = new ArrayList<>();
preparePoker(pokerMap, pokerNum);
//洗牌
Collections.shuffle(pokerNum);
//System.out.println(pokerNum);
//发牌
TreeSet<Integer> player1 = takePoker(pokerNum,1);
TreeSet<Integer> player2 = takePoker(pokerNum,2);
TreeSet<Integer> player3 = takePoker(pokerNum,3);
TreeSet<Integer> dp = takePoker(pokerNum,0);
// System.out.println(player1);
// System.out.println(player2);
// System.out.println(player3);
// System.out.println(dp);
//看牌
lookPoker("zhangsan", player1, pokerMap);
lookPoker("lisi", player2, pokerMap);
lookPoker("wangwu", player3, pokerMap);
lookPoker("底牌", dp, pokerMap);
}
//准备牌
public static void preparePoker(Map<Integer, String> pokerMap, List<Integer> pokerNum) {
String[] colors = {"♠", "♥", "♣", "♦"};
String[] numbers = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};
int index = 0;
pokerMap.put(index, "大王");
pokerNum.add(index);
index++;
pokerMap.put(index, "小王");
pokerNum.add(index);
index++;
for (int i = 0; i < numbers.length; i++) {
for (int j = 0; j < colors.length; j++) {
pokerMap.put(index, colors[j] + numbers[i]);
pokerNum.add(index);
index++;
}
}
//System.out.println(pokerMap);
}
//发牌
public static TreeSet<Integer> takePoker(List<Integer> pokerNum,Integer id) {
TreeSet<Integer> player1 = new TreeSet<>();
TreeSet<Integer> player2 = new TreeSet<>();
TreeSet<Integer> player3 = new TreeSet<>();
TreeSet<Integer> dp = new TreeSet<>();//底牌
for (int i = 0; i < pokerNum.size(); i++) {
if (i > pokerNum.size() - 4) {
dp.add(pokerNum.get(i));
} else if (i % 3 == 0) {
player1.add(pokerNum.get(i));
} else if (i % 3 == 1) {
player2.add(pokerNum.get(i));
} else if (i % 3 == 2) {
player3.add(pokerNum.get(i));
}
}
if (id == 0){
return dp;
} else if (id == 1) {
return player1;
}else if (id == 2){
return player2;
} else if (id == 3) {
return player3;
}
return new TreeSet<>();
}
//看牌
public static void lookPoker(String name, TreeSet<Integer> player, Map<Integer, String> pokerMap) {
System.out.println(name + "的牌是:");
for (Integer pokerNum : player) {
String poker = pokerMap.get(pokerNum);
System.out.print(poker + " ");
}
System.out.println();
}
日志框架
介绍
程序中的日志可以用来记录程序中运行过程中的信息,并可以进行永久存储
优势
- 可以将系统执行的信息选择性的记录到指定的位置(控制台,文件,数据库中)
- 可以随时以开关的形式控制是否记录日志,无需修改源代码
体系结构
Logback
概述
快速入门