case1: 集合、Random随机数、多态、封装
ShootOddPeople类
package com.zz.examine.case1;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
*
*/
public class ShootOddPerson {
public static List<People> peoples = new ArrayList <>();
public static void main(String args[]){
//目标:完成幸存者案例
/**
* 1.由于每个囚犯是一个对象,
* 并且我们需要每个囚犯的编号和 位置,
* 所以我们需要将囚犯封装成对象.
* 2.创建100个囚犯,依次站位,并为其赋值编号(1-200)的随机数,不能重复
*/
//3.循环100次,创建100个囚犯,存入集合
Random r = new Random();
for(int i = 1;i <= 100; i++){
while(true){
int code = r.nextInt(200) + 1;
//判断编号是否唯一
if(exist(code)) {
People p = new People(code, i);
peoples.add(p);
break;//代表当前囚犯添加成功
}
}
}
System.out.println("囚犯站位" + peoples);
//4.反复删除奇数位置处的人,知道剩余一个人为止
//peoples = [p1,p2,p3,.....]
while(peoples.size() > 1){
//干掉奇数位置的人:逆向思维--留下偶数位置处的人
List<People> tempPeoples = new ArrayList <>();
for(int i = 1; i < peoples.size(); i+=2){
People p = peoples.get(i);
tempPeoples.add(p);
}
peoples = tempPeoples;
}
People luckPeople = peoples.get(0);
System.out.println(luckPeople);
}
//判断编号是否存在
public static boolean exist(int code){
for(People people:peoples){
if(people.getCode() == code)
return false;//重复了,不能用
}
return true;
}
}
People类
package com.zz.examine.case1;
public class People {
private int code;//编号
private int location;//位置
public People() {
}
public People(int code, int location) {
this.code = code;
this.location = location;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public int getLocation() {
return location;
}
public void setLocation(int location) {
this.location = location;
}
@Override
public String toString() {
return "People{" + "code=" + code + ", location=" + location + '}';
}
}
case2:LocalDate ,Map集合,split方法分割,
Test类
package com.zz.examine.case2;
import com.sun.javafx.image.IntPixelGetter;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String args[]){
//1.把数据拿到程序中
String userStrs = "1001:张三:男:1998-02-23#1002:李四:女:1849-12-12#1003:王五:男:2000-12-12#1003:张三:女:2010-12-12";
//2.定义一个用户类
//3.提前定义一个List<User>集合用于存储用户集合
List<User> users = new ArrayList <>();
//4.把全部用户数据,用#分割
String[] userStrArray = userStrs.split("#");
for (String userStr : userStrArray) {
//userStr = "1001:张三:男:1998-02-23"
//5.创建一个用户对象,封装这个用户数据
User user = new User();
String[] userInfo = userStr.split(":");
user.setId(Long.valueOf(userInfo[0]));
user.setName(userInfo[1]);
user.setGender(userInfo[2]);
user.setBirthday(LocalDate.parse(userInfo[3]));
//6.把这个用户存入到集合总
users.add(user);
}
System.out.println(users);
System.out.println("============业务二===========");
//7.遍历List集合中的用户对象,统计相同名称出现的次数
Map<String, Integer> result = new HashMap <>();
for (User user : users) {
String name = user.getName();
if(result.containsKey(name)){
result.put(name, result.get(name) + 1);
}else{
result.put(name, 1);
}
}
//8.遍历Map集合
result.forEach((k,v)->{
System.out.println(k + ":" + v + "次");
});
}
}
User类
package com.zz.examine.case2;
import java.time.LocalDate;
public class User {
private Long id;
private String name;
private String gender;//性别
private LocalDate birthday;//生日
public User() {
}
public User(Long id, String name, String gender, LocalDate birthday) {
this.id = id;
this.gender = gender;
this.birthday = birthday;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public LocalDate getBirthday() {
return birthday;
}
public void setBirthday(LocalDate birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" + "id=" + id + ", name='" + name + '\'' + ", gender='" + gender + '\'' + ", birthday=" + birthday + '}';
}
}