Day16
2023-12-19 06:02:14
Day16
一,大数值运算类
1,整数的大数值运算
//导包
import java.math.BigInteger;
public class Test01 {
/**
* 知识点:BigInteger - 整数的大数值运算类
*/
public static void main(String[] args) {
BigInteger big1 = new BigInteger("12345678901234567890");
BigInteger big2 = new BigInteger("12345678901234567890");
BigInteger add = big1.add(big2);
System.out.println("加法:" + add);
BigInteger subtract = big1.subtract(big2);
System.out.println("减法:" + subtract);
BigInteger multiply = big1.multiply(big2);
System.out.println("乘法:" + multiply);
BigInteger divide = big1.divide(big2);
System.out.println("除法:" + divide);
}
}
2,小数的大数值运算
import java.math.BigDecimal;
public class Test02 {
/**
* 知识点:BigDecimal - 小数的大数值运算类
*/
public static void main(String[] args) {
//导入字符串类的数据,避免数据丢失精度
BigDecimal big1 = new BigDecimal("0.5");
BigDecimal big2 = new BigDecimal("0.4");
BigDecimal add = big1.add(big2);
System.out.println("加法:" + add);
BigDecimal subtract = big1.subtract(big2);
System.out.println("减法:" + subtract);
BigDecimal multiply = big1.multiply(big2);
System.out.println("乘法:" + multiply);
BigDecimal divide = big1.divide(big2);
System.out.println("除法:" + divide);
}
}
import java.math.BigDecimal;
public class Test03 {
/**
* 知识点:BigDecimal - 小数的大数值运算类
*/
public static void main(String[] args) {
BigDecimal big1 = new BigDecimal("10");
BigDecimal big2 = new BigDecimal("3");
//遇到余数为无限循环小数时,可以采用提取小数点后两位,四舍五入
BigDecimal divide = big1.divide(big2, 2, BigDecimal.ROUND_HALF_UP);
System.out.println("除法:" + divide);
}
}
二,枚举
1,枚举的用法
不用枚举:
public class Test01{
/**
* 知识点:枚举引入案例
*
* 需求:编写季节类(Season),该类只有四个对象(spring,summer,autumn,winter)
* 步骤:
* 1.私有化Season的构造方法 -- 目的:让外界不能创建对象
* 2.在类中创建四个对象(spring,summer,autumn,winter)
* 3.使用public修饰 -- 目的:扩大访问权限
* 4.使用static修饰 -- 目的:通过类名调用
* 5.使用final修饰 --- 目的:外界不能改动对象
*
* 枚举的应用场景:一个类只能有固定的几个对象,就使用枚举代替
*/
public static void main(String[] args) {
System.out.println(Season.spring);
System.out.println(Season.summer);
System.out.println(Season.autumn);
System.out.println(Season.winter);
}
}
public class Season {
public static final Season spring = new Season("春天", "万物复苏");
public static final Season summer = new Season("夏天", "汗如雨下");
public static final Season autumn = new Season("秋天", "秋高气爽");
public static final Season winter = new Season("冬天", "银装素裹");
private String name;
private String info;
private Season() {
}
private Season(String name, String info) {
this.name = name;
this.info = info;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return "Season [name=" + name + ", info=" + info + "]";
}
}
采用枚举:
public enum Color { //枚举里没有定义属性、方法,可以在最后一个对象后面加逗号、分号或什么都不加 RED,GREEN,BLUE; }
//隐性继承于Enum类
public enum Season{
//public static final Season spring = new Season("春天","春雨绵绵");
spring("春天","春雨绵绵"),
summer("夏天","烈日炎炎"),
autumn("秋天","硕果累累"),
winter("冬天","白雪皑皑");
private String name;
private String info;
private Season() {
}
private Season(String name, String info) {
this.name = name;
this.info = info;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
@Override
public String toString() {
return this.name + " -- " + this.info;
}
}
//测试一:
public class Test01 {
/**
* 知识点:枚举案例
*
* 需求:编写季节类(Season),该类只有四个对象(spring,summer,autumn,winter)
* 注意:使用枚举解决该需求
*/
public static void main(String[] args) {
System.out.println(Season.spring);
System.out.println(Season.summer);
System.out.println(Season.autumn);
System.out.println(Season.winter);
}
}
枚举的常用方法:
public class Test02 {
/**
* 知识点:枚举的常用方法
*/
public static void main(String[] args) {
//通过对象名获取到指定枚举类中的对象
Season season1 = Enum.valueOf(Season.class, "spring");
System.out.println(season1);
//通过对象名获取到指定枚举类中的对象
Season season2 = Season.valueOf("spring");
System.out.println(season2);
//获取枚举类中所有的对象,返回对象数组
Season[] values = Season.values();
for (Season season : values) {
System.out.println(season);
}
}
}
扩展:
public class Test03 {
/**
* 扩展:switch如何处理枚举
*/
public static void main(String[] args) {
switch (Season.spring) {
case spring:
System.out.println("春天");
break;
case summer:
System.out.println("夏天");
break;
case autumn:
System.out.println("秋天");
break;
case winter:
System.out.println("冬天");
break;
}
}
}
public class Test04 {
/**
* 扩展:switch如何处理字符串
*/
public static void main(String[] args) {
switch ("C#") {
case "BB":
System.out.println("BB");
break;
case "Aa":
System.out.println("Aa");
break;
case "CC":
System.out.println("CC");
break;
}
}
}
2,枚举的案例
2.1,状态机
import java.util.Scanner;
public class Test01 {
/**
* 知识点:枚举案例 -- 状态机
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请选择信号灯:RED,YELLOW,GREEN");
String str = scan.next();
Signal signal = Signal.valueOf(str);
String trafficInstruct = getTrafficInstruct(signal);
System.out.println(trafficInstruct);
scan.close();
}
public static String getTrafficInstruct(Signal signal){
String instruct = "信号灯故障";
switch (signal) {
case RED:
instruct = "红灯停";
break;
case YELLOW:
instruct = "黄灯请注意";
break;
case GREEN:
instruct = "绿灯行";
break;
}
return instruct;
}
}
//信号灯的枚举
public enum Signal {
RED,YELLOW,GREEN;
}
2.2,错误码
public class Test01 {
/**
* 知识点:枚举案例 -- 错误码
*/
public static void main(String[] args) {
System.out.println(AddCode.ERR_1);
System.out.println(AddCode.ERR_2);
System.out.println(AddCode.OK);
}
}
public enum AddCode {
ERR_1(-1,"添加失败-学生信息不合法"),
ERR_2(-2,"添加失败-有重复学生"),
OK(1,"添加成功");
private int code;
private String message;
private AddCode() {
}
private AddCode(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return message;
}
}
2.3,组织枚举
public class Test01 {
/**
* 知识点:枚举案例 -- 组织枚举
*
* 经验:项目中会把相同的枚举使用类或接口组织起来,一般使用接口组织
* 使用类组织枚举,枚举自动加上static
* 使用接口组织枚举,枚举自动加上public static
*/
public static void main(String[] args) {
System.out.println(Code.AddCode.ERR_1);
}
}
public class Code {
enum AddCode {
ERR_1(-1,"添加失败-学生信息不合法"),
ERR_2(-2,"添加失败-有重复学生"),
OK(1,"添加成功");
private int code;
private String message;
private AddCode() {
}
private AddCode(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return message;
}
}
enum DeleteCode {
ERR_1(-1,"删除失败-学生信息不合法"),
ERR_2(-2,"删除失败-没有该学生"),
OK(1,"删除成功");
private int code;
private String message;
private DeleteCode() {
}
private DeleteCode(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return message;
}
}
enum UpdateCode {
ERR_1(-1,"修改失败-学生信息不合法"),
ERR_2(-2,"修改失败-没有该学生"),
ERR_3(-3,"修改失败 -- 修改类型异常"),
ERR_4(-4,"修改失败 -- 修改数据不合法"),
ERR_5(-5,"修改失败 -- 目标班级上已有学生"),
ERR_6(-6,"修改失败 -- 目标学号上已有学生"),
OK(1,"修改成功");
private int code;
private String message;
private UpdateCode() {
}
private UpdateCode(int code, String message) {
this.code = code;
this.message = message;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return message;
}
}
}
2.4,案例枚举
public class Test01 {
/**
* 知识点:枚举案例 -- 策略枚举
*
* 需求:制作千锋计算工资的功能
* 分析:
* 员工分类:行政人员、讲师、招生
* 部门:
* 总经办(行政人员)、品质保障部(行政人员)、就业老师(行政人员)
* Java(讲师)、Python(讲师)、HTML(讲师)
* 院校(招生)、网咨(招生)、职发(招生)
*/
public static void main(String[] args) {
double salary1 = Salary.generalManagerOffice.getSalary(20000, 0, 0, 20000, 0);
System.out.println("计算校长的工资:" + salary1);
double salary2 = Salary.qualityAssurance.getSalary(10000, 0, 0, 8000, 0);
System.out.println("计算班主任的工资:" + salary2);
double salary3 = Salary.academy.getSalary(6000, 0, 0, 4000, 4000);
System.out.println("计算院校招生老师的工资:" + salary3);
double salary4 = Salary.java.getSalary(1200, 88, 10, 200, 0);
System.out.println("计算Java讲师的工资:" + salary4);
}
}
import java.math.BigDecimal;
//获取部门工资的枚举
public enum Salary {
java(StaffType.teacher),python(StaffType.teacher),HTML(StaffType.teacher),
generalManagerOffice(StaffType.administration),qualityAssurance(StaffType.administration),obtainEmployment(StaffType.administration),
academy(StaffType.recruitStudents),onlineConsultation(StaffType.recruitStudents),jobDevelopment(StaffType.recruitStudents);
private StaffType staffType;
private Salary(StaffType staffType) {
this.staffType = staffType;
}
/**
* 获取工资
* @param baseSalary 基本工资
* @param classHour 课时
* @param teachingHourSubsidy 课时费
* @param achievements 绩效
* @param recruitStudentsAchievements 招生绩效
* @return 工资
*/
//获取工资
public double getSalary(double baseSalary,int classHour,double teachingHourSubsidy,double achievements,double recruitStudentsAchievements){
return staffType.calculateSalary(baseSalary, classHour, teachingHourSubsidy, achievements, recruitStudentsAchievements);
}
//员工类别的枚举
private enum StaffType{
//底层创建了一个匿名类(Salary$StaffType$1),继承StaffType
//创建的是匿名类(Salary$StaffType$1)的对象 -- administration
administration {
@Override
public double calculateSalary(double baseSalary, int classHour, double teachingHourSubsidy,
double achievements, double recruitStudentsAchievements) {
BigDecimal big1 = new BigDecimal(String.valueOf(baseSalary));
BigDecimal big2 = new BigDecimal(String.valueOf(achievements));
BigDecimal result = big1.add(big2);
return result.doubleValue();
}
},
teacher {
@Override
public double calculateSalary(double baseSalary, int classHour, double teachingHourSubsidy,
double achievements, double recruitStudentsAchievements) {
BigDecimal big1 = new BigDecimal(String.valueOf(baseSalary));
BigDecimal big2 = new BigDecimal(String.valueOf(achievements));
BigDecimal big3 = new BigDecimal(String.valueOf(classHour));
BigDecimal big4 = new BigDecimal(String.valueOf(teachingHourSubsidy));
BigDecimal result = big3.multiply(big4).add(big1).add(big2);
return result.doubleValue();
}
},
recruitStudents {
@Override
public double calculateSalary(double baseSalary, int classHour, double teachingHourSubsidy,
double achievements, double recruitStudentsAchievements) {
BigDecimal big1 = new BigDecimal(String.valueOf(baseSalary));
BigDecimal big2 = new BigDecimal(String.valueOf(achievements));
BigDecimal big3 = new BigDecimal(String.valueOf(recruitStudentsAchievements));
BigDecimal result = big1.add(big2).add(big3);
return result.doubleValue();
}
};
public abstract double calculateSalary(double baseSalary,int classHour,double teachingHourSubsidy,double achievements,double recruitStudentsAchievements);
}
}
三,集合
1,ArraysList
1.1,初识ArrayList
import java.util.ArrayList;
public class Test01 {
/**
* 知识点:初识ArrayList
*/
public static void main(String[] args) {
//创建ArrayList对象
ArrayList list = new ArrayList();
//添加元素
list.add(100);//Intger.valueOf(100);
list.add(123.123);//Double.valueOf(123.123);
list.add("用良心做教育");
//注意:虽然一个集合对象中可以存储多种数据类型的元素,但是不方便管理,实现使用中一个集合对象里还是只存储同一种类型的元素
}
}
1.2,ArrayList的使用
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class Test02 {
/**
* 知识点:ArrayList的使用
*/
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
//添加元素
list.add("麻生希");
list.add("椎名空");
list.add("水菜丽");
list.add("朝桐光");
list.add("樱井步");
list.add("周隽乐");
//将元素添加到指定下标上
list.add(5, "严荐翔");
ArrayList<String> newList1 = new ArrayList<>();
Collections.addAll(newList1, "aaa","bbb","ccc","ccc");//利用Collections(集合工具类)进行批量添加
list.addAll(newList1);//将newList1中所有的元素添加到list的末尾
ArrayList<String> newList2 = new ArrayList<>();
Collections.addAll(newList2, "xxx","yyy","zzz","zzz");//利用Collections(集合工具类)进行批量添加
list.addAll(5,newList2);//将newList2中所有的元素添加到list的指定下标的位置
//设置指定下标上的元素
list.set(2, "罗仁君");
//获取指定下标上的元素
String string = list.get(2);
System.out.println("获取指定下标上的元素:" + string);
//获取元素个数
int size = list.size();
System.out.println("获取元素个数:" + size);
//清空集合中所有的元素
//list.clear();
System.out.println("判断集合中是否包含某个元素:" + list.contains("严荐翔"));//true
System.out.println("判断集合中是否包含某个子集合:" + list.containsAll(newList1));//true
System.out.println("获取元素第一次出现的下标:" + list.indexOf("ccc"));//13
System.out.println("获取元素最后一次出现的下标:" + list.lastIndexOf("ccc"));//14
//true - 没有元素,fasle - 有元素
System.out.println("判断集合是否没有元素:" + list.isEmpty());//false
list.remove(4);//依据下标删除元素
list.remove("朝桐光");//依据元素删除元素
list.removeAll(newList2);//删除交集
//从开始下标处(包含)截取到结束下标处(排他),返回新的集合
List<String> subList = list.subList(2, 8);
Object[] objArr = subList.toArray();//将集合转换为数组
System.out.println(Arrays.toString(objArr));//把数组转换为字符串
String[] strArr = new String[subList.size()];
subList.toArray(strArr);//将集合转换为指定类型的数组
System.out.println(Arrays.toString(strArr));
//保留交集
list.retainAll(newList1);
System.out.println("------------------");
//遍历集合 -- for循环
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
System.out.println("------------------");
//遍历集合 -- foreach
for (String element : list) {
System.out.println(element);
}
System.out.println("------------------");
//遍历集合 -- Iterator
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){//判断是否有可迭代的元素
String element = iterator.next();
System.out.println(element);
}
System.out.println("------------------");
//遍历集合 -- ListIterator
ListIterator<String> listIterator = list.listIterator();
while(listIterator.hasNext()){
String element = listIterator.next();
System.out.println(element);
}
}
}
1.3,ArrayList中的泛型使用
import java.util.ArrayList;
public class Test01 {
/**
* 知识点:ArrayList中的泛型使用
*/
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
System.out.println("----------------");
MyArrayList<String> myArrayList = new MyArrayList<>();
myArrayList.add("aaa");
myArrayList.add("bbb");
}
}
/**
* E - element - 元素
* T - Type - 类型
* N - name - 名
* K - Key - 键
* V - value - 值
* 注意:泛型可以有多个,用逗号隔开
*/
public class MyArrayList<E> {
public void add(E e){
}
}
2,泛型
2.1泛型在接口中的使用
public class Test01 {
/**
* 知识点:泛型在接口中的使用
*
* 需求:编写通用的管理系统的接口
*/
public static void main(String[] args) {
//图书管理系统 -- 管理的是图书的对象
BookManagerSystem<Book> bookManagerSystem1 = new BookManagerSystem<>();
bookManagerSystem1.add(new Book());
//图书管理系统 -- 管理的是书名
BookManagerSystem<String> bookManagerSystem2 = new BookManagerSystem<>();
bookManagerSystem2.add("Java从入门到精通");
bookManagerSystem2.add("MySQL从删库到跑路");
}
}
public interface IManagerSystem<T> {
public int add(T t);
}
public class BookManagerSystem<T> implements IManagerSystem<T>{
@Override
public int add(T t) {
// TODO Auto-generated method stub
return 0;
}
}
public class StudentManagerSystem implements IManagerSystem<Student>{
@Override
public int add(Student t) {
// TODO Auto-generated method stub
return 0;
}
}
class Book {
}
class Student {
}
2.2,泛型的限定
import java.util.ArrayList;
public class Test01 {
/**
* 知识点:泛型限定
*/
public static void main(String[] args) {
}
//?表示所有的类型
public static ArrayList<?> method01(){
// ArrayList<Object> list = new ArrayList<>();
// ArrayList<A> list = new ArrayList<>();
ArrayList<B> list = new ArrayList<>();
return list;
}
//? extends A 表示A类型或A的子类类型
public static ArrayList<? extends A> method02(){
// ArrayList<A> list = new ArrayList<>();
ArrayList<B> list = new ArrayList<>();
return list;
}
//? super A 表示A类型或A的父类类型
public static ArrayList<? super A> method03(){
// ArrayList<A> list = new ArrayList<>();
ArrayList<Object> list = new ArrayList<>();
return list;
}
}
public class A{}
public class B extends A{}
文章来源:https://blog.csdn.net/haikeydnk/article/details/135069996
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!