BigDecimal
-
作用:可以用来进行精确计算
-
构造方法:
方法名 | 说明 |
---|---|
BigDecimal(double val) | 参数为double |
BigDecimal(String val) | 参数为String |
- 常用方法
方法名 | 说明 |
---|---|
public BigDecimal add(另一个BigDecimal对象) | 加法 |
public BigDecimal subtract (另一个BigDecimal对象) | 减法 |
public BigDecimal multiply (另一个BigDecimal对象) | 乘法 |
public BigDecimal divide (另一个BigDecimal对象) | 除法 |
public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式) | 除法 |
-
总结
-
BigDecimal是用来进行精确计算的
-
创建BigDecimal的对象,构造方法使用参数类型为字符串的。
-
四则运算中的除法,如果除不尽请使用divide的三个参数的方法。
-
代码1:如果想要进行精确运算,那么请使用字符串的构造
// BigDecimal bd1 = new BigDecimal(0.1);
// BigDecimal bd2 = new BigDecimal(0.2);
//如果想要进行精确运算,那么请使用字符串的构造
BigDecimal bd1 = new BigDecimal("0.1");
BigDecimal bd2 = new BigDecimal("0.2");
// public BigDecimal add(另一个BigDecimal对象) 加法
BigDecimal add = bd1.add(bd2);
System.out.println("和为" + add);
// public BigDecimal subtract (另一个BigDecimal对象) 减法
BigDecimal subtract = bd1.subtract(bd2);
System.out.println("差为" + subtract);
// public BigDecimal multiply (另一个BigDecimal对象) 乘法
BigDecimal multiply = bd1.multiply(bd2);
System.out.println("积为" + multiply);
// public BigDecimal divide (另一个BigDecimal对象) 除法
BigDecimal divide = bd1.divide(bd2);
System.out.println("商为"+divide);
代码2 如果除不尽请使用divide的三个参数的方法。
BigDecimal divide = bd1.divide(参与运算的对象,小数点后精确到多少位,舍入模式);
//参数一:表示参数运算的另一个对象
//参数二:表示小数点后精确到多少位
//参数三:舍入模式
//进一法 BigDecimal.ROUND_UP 理解为向上舍入
//去尾法 BigDecimal.ROUND_FLOOR 理解为向下舍入
//四舍五入 BigDecimal.ROUND_HALF_UP
BigDecimal bd01 = new BigDecimal("1.0");
BigDecimal bd02 = new BigDecimal("3.0");
// 没有精准结果 会报错 ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
//BigDecimal result = bd01.divide(bd02);
//0.33333
BigDecimal result1 = bd01.divide(bd02, 3, BigDecimal.ROUND_UP);
System.out.println(result1);
BigDecimal result2 = bd01.divide(bd02, 3,BigDecimal.ROUND_FLOOR);
System.out.println(result2);
BigDecimal result3 = bd01.divide(bd02, 3,BigDecimal.ROUND_HALF_UP);
System.out.println(result3);
BigDecimal d05 = new BigDecimal("10");
BigDecimal d06 = new BigDecimal("7");
System.out.println(d05.divide(d06, 2, BigDecimal.ROUND_UP)); //1.4285 1.43
System.out.println(d05.divide(d06, 2, BigDecimal.ROUND_FLOOR)); //1.42
System.out.println(d05.divide(d06, 2, BigDecimal.ROUND_HALF_UP)); //1.43
包装类
int Integer
float Float
double Double
char Character kærəktər 开弱科特
byte Byte
short Short
long Long
boolean Boolean
Integer
-
创建Integer对象的方式 ,使用构造方法或者valueOf方法
-
在-128到127之间的数,都会被缓存,使用Integer.valueOf初始化只存在一个对象
// public Integer(int value) 根据int创建Integer对象(过时)
//public Integer(String s) 根据String值创建Integer对象(过时)
Integer i1 = new Integer(100);
Integer i2 = new Integer("100");
System.out.println(i1);
System.out.println(i2);
//public static Integer valueOf(int i) 返回表示指定的int值的Integer实例
//public static Integer valueOf(String s) 返回一个保存指定值的Integer对象String
Integer i3 = Integer.valueOf(100);
Integer i4 = Integer.valueOf("200");
System.out.println(i3);
System.out.println(i4);
//在-128到127之间的数,都会被缓存,使用Integer.valueOf初始化只存在一个对象
System.out.println(Integer.valueOf(100) == Integer.valueOf(100));//true
System.out.println(new Integer(100) == new Integer(100));//false
自动装箱和拆箱(掌握)
装箱
-
把基本数据类型转换为包装类类型 jdk1.5的特性
拆箱
-
把包装类类型转换为基本数据类型 jdk1.5的特性
Integer a01 = 100; //自动装箱 把int类型变为Integer
System.out.println(a01);
Integer a02 = Integer.valueOf(200);
int a2 = a02;// 自动拆箱 把Integer转为int
System.out.println(a2);
a02 += 300;
//a02 = a02+300;
//1 先把a02拆箱 得到int类型的200
//2 200+300 计算 500
//3 把int类型的500 装箱 得到了a02
System.out.println(a02);
String转int(掌握)
-
Integer.parseInt()
String str01 = "123";
//字符串转int
int i1 = Integer.parseInt(str01);
System.out.println(i1);
int i2 = Integer.parseInt("100");
System.out.println(i2);
//字符串转Integer Integer.valueOf得到的是Integer对象
Integer integer01 = Integer.valueOf("200");
int转String(掌握)
-
1和一空字符串用加号拼接
-
2String.valueOf
int b = 300;
//int转字符串 方法1 直接和空字符串拼接
System.out.println(b + "");
//使用String.valueOf方法 得到字符串
String s1 = String.valueOf(b);
System.out.println(s1);
Arrays
-
Arrays.toString(); //返回指定数组的内容的字符串表示形式 Arrays.sort(); //public static void sort(int[] a)
-
数组长度为n,则1 <= n < 47 使用插入排序数组长度为n,
-
则47 <= n < 286 使用使用快速排序数组长度为n,
-
则286 < n 使用归并排序或快速排序(有一定顺序使用归并排序,毫无顺序使用快排)
-
-
Arrays.binarySearch() 二分查找返回指定元素的索引
//public static String toString(int[] a) 返回指定数组的内容的字符串表示形式
int[] arr = new int[]{35, 77, 8, 6, 34, 546, 86, 9, 645, 3,};
//Arrays.toString 拼接数组数据并且返回
System.out.println(Arrays.toString(arr));
//从小到大排序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
//二分查找 返回数据的索引位置
//1,数组必须有序
//2.如果要查找的元素存在,那么返回的是这个元素实际的索引
//3.如果要查找的元素不存在,那么返回的是 (-插入点-1)
//插入点:如果这个元素在数组中,他应该在哪个索引上.
int index = Arrays.binarySearch(arr, 86);
System.out.println(index);
//-8 如果数据找不到 会找到数据应该在的索引位置index 返回的是 -index-1
int index1 = Arrays.binarySearch(arr, 10);//-8
System.out.println(index1);
冒泡排序
-
一些效率高的排序 :快排 希尔排序 归并排序 堆排序 桶排序 timsort
public static void main(String[] args) {
int[] arr = new int[]{33, 10, 50, 40, 39, 8, 1};
sort(arr);
//Arrays.toString 可以打印数组内容
System.out.println(Arrays.toString(arr));
}
/**
* 冒泡
* 1从左向右 每两个数据依次比较,如果左边比右边的大,交换两个数据
* 2 按照第1步比较一轮之后,会得到一个最大值,在数组的最右侧
* 3 把剩余的数据按照第一步继续执行,依次次得到每一轮的最大值,如果有n个数据 比较n-1轮就结束了
* @param arr
* @return
*/
public static void sort(int[] arr) {
//比较 n-1轮 n是数组长度 {33, 10, 50, 40, 39, 8, 1}
// 长度是7 循环6轮 每一次 都要获取到一个最大值
//外层循环 控制遍历几轮 也就是找出几个最大值
for (int i = 0; i < arr.length - 1; i++) {
//里层循环 控制每一轮 要比较几次才能找出这个最大值
for (int j = 0; j < arr.length - 1 - i; j++) {
//从左向右 两两比较数据 左边的如果大于右边 交换两个数据
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
递归
递归的介绍
传递和回归
-
以编程的角度来看,递归指的是方法定义中调用方法本身的现象
-
把一个复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解
-
递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算
从1+n的和
public static void main(String[] args) {
//从1+n的和
//1---100
//100 + (1到99的和)
//(1到99的和)
// 99+ (1到98的和)
//(1到98的和)
//98+ (1到97的和) .........
//(1到2和和)
//2 + (1到1的和)1
int result = sum(4);
System.out.println(result);
}
public static int sum(int num) {
//计算1到num的和
if (num == 1) {
return num;
} else {
return num + sum(num - 1);
}
}
求阶乘
public static void main(String[] args) {
//阶乘 5! = 5*4*3*2*1
// n! n(n-1)(n-2)*....*1
System.out.println(factorial(5));
}
//阶乘的
public static int factorial(int num) {
if (num == 1) {
return 1;
} else {
return num * factorial(num - 1);
}
}
时间类
Date时间相关API
1Date类
-
计算机中时间原点
1970年1月1日 00:00:00
-
时间换算单位
1秒 = 1000毫秒
-
Date类概述
Date 代表了一个特定的时间,精确到毫秒
-
Date类构造方法
方法名 | 说明 |
---|---|
public Date() | 分配一个 Date对象,并初始化,以便它代表它被分配的时间,精确到毫秒 |
public Date(long date) | 分配一个 Date对象,并将其初始化为表示从标准基准时间起指定的毫秒数 |
public class DateDemo01 {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);//Fri Sep 30 10:54:06 CST 2022
//如果传入的毫秒值 会从1970-01-01 08:00:00 累加这个毫秒值 得到的时间
Date date1 = new Date(24 * 60 * 60 * 1000);
System.out.println(date1);//Fri Jan 02 08:00:00 CST 1970
}
}
Date类常用方法
-
常用方法
方法名 说明 public long getTime() 获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值 public void setTime(long time) 设置时间,给的是毫秒值 -
示例代码
public class DateDemo02 {
public static void main(String[] args) {
//创建日期对象
Date d = new Date();
//public long getTime():获取的是日期对象从1970年1月1日 00:00:00到现在的毫秒值
// System.out.println(d.getTime());
// System.out.println(d.getTime() * 1.0 / 1000 / 60 / 60 / 24 / 365 + "年");
//public void setTime(long time):设置时间,给的是毫秒值 会从1970-01-01 08:00:00 累加这个毫秒值 得到的时间
// long time = 1000*60*60;
d.setTime(time);
System.out.println(d);
}
}
SimpleDateFormat类(掌握掌握掌握掌握)
-
SimpleDateFormat类概述
SimpleDateFormat是一个具体的类,用于以区域设置敏感的方式格式化和解析日期。
我们重点学习日期格式化和解析
-
SimpleDateFormat类构造方法
方法名 说明 public SimpleDateFormat() 构造一个SimpleDateFormat,使用默认模式和日期格式 public SimpleDateFormat(String pattern) 构造一个SimpleDateFormat使用给定的模式和默认的日期格式 -
SimpleDateFormat类的常用方法
-
格式化(从Date到String)
-
public final String format(Date date):将日期格式化成日期/时间字符串
-
-
解析(从String到Date)
-
public Date parse(String source):从给定字符串的开始解析文本以生成日期
-
-
-
示例代码
public class SimpleDateFormatDemo {
public static void main(String[] args) throws ParseException {
//格式化:从 Date 到 String
Date d = new Date();
// SimpleDateFormat sdf = new SimpleDateFormat();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s = sdf.format(d);
System.out.println(s);
System.out.println("--------");
//从 String 到 Date
String ss = "2048-08-09 11:11:11";
//ParseException
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dd = sdf2.parse(ss);
System.out.println(dd);
}
}
小结
new Date();
new Date(3600L * 1000); 传入的是毫秒值
Date d = new Date();
d.setTime(3600L * 1000);作用设置毫秒值时间
d.getTime(); 获取毫秒值
//EEEE是星期几 如过显示周几 写一个E
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss------EEEE");
sdf.format(date) 把date对象转为指定格式的字符串
sdf.parse("2048-01-01") 把符合格式的字符串转为date对象
案例:判断小贾 和小皮的时间有没有中奖
public static void main(String[] args) throws ParseException {
String start = "2020年11月11日 0:0:0"; //秒杀活动开始时间
String end = "2020年11月11日 0:10:0"; //秒杀活动结束时间
String jia = "2020年11月11日 0:03:47";
String pi = "2020年11月11日 0:10:11";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//解析4个字符串 时间
Date startDate = sdf.parse(start);
Date endDate = sdf.parse(end);
Date jiaDate = sdf.parse(jia);
Date piDate = sdf.parse(pi);
//获取4个时间 的毫秒值
long startTime = startDate.getTime();
long endTime = endDate.getTime();
long jiaTime = jiaDate.getTime();
long piTime = piDate.getTime();
if (jiaTime >= startTime && startTime <= endTime) {
System.out.println("小贾参与秒杀成功 ");
} else {
System.out.println("小贾参与秒杀失败 ");
}
if (piTime >= startTime && piTime <= endTime) {
System.out.println("小皮参与秒杀成功 ");
} else {
System.out.println("小皮参与秒杀失败 ");
}
}
集合(存储数据的容器)
-
集合长度可变
-
集合只能存引用数据类型,如果要存基本数据类型,需要存对应的包装类
Collection集合常用方法
方法名 | 说明 |
---|---|
boolean add(E e) | 添加元素 |
boolean remove(Object o) | 从集合中移除指定的元素 |
boolean removeIf(Object o) | 根据条件进行移除 |
void clear() | 清空集合中的元素 |
boolean contains(Object o) | 判断集合中是否存在指定的元素 |
boolean isEmpty() | 判断集合是否为空 |
int size() | 集合的长度,也就是集合中元素的个数 |
迭代器
-
iterate迭代 iterator迭代器
-
迭代器:用来遍历集合用的
获取集合的迭代器对象 iterator()方法
iterator(): 返回此集合中元素的迭代器,通过集合对象的iterator()方法得到
迭代器遍历集合的代码
-
一个新的迭代器对象,一开始默认指向集合中第0个数据
-
hasNext()方法 可以判断当前迭代器指向的集合的位置是否有数据
-
next()方法 取出当前位置的元素 并且 将迭代器往后移动一个位置
Collection<String> ll = new ArrayList<>();
ll.add("aaa");
ll.add("bbb");
ll.add("cc");
ll.add("dddd");
ll.add("eee");
//获取集合的迭代器 对象 ,一开始默认指向第0个元素
Iterator<String> it = ll.iterator();
//next: 1获取当前指向的数据 ,2然后指向下一个数据
//String s01 = it.next();
//System.out.println(s01);
//System.out.println(it.next());
//System.out.println(it.next());
//System.out.println(it.next());
//System.out.println(it.next());
//System.out.println(it.next());
//判断指向的数据是否存在
//if (it.hasNext()) {
// System.out.println(it.next());
//}
while (it.hasNext()){
System.out.println(it.next());
}
迭代器循环中删除
-
remove,会删除 迭代器上一次调用next获取的数据
代码
ArrayList<String> al = new ArrayList<>();
al.add("aa");
al.add("bb");
al.add("cc");
al.add("cc");
al.add("cc");
al.add("cc");
al.add("dd");
al.add("ee");
al.add("cc");
al.add("cc");
System.out.println(al);
Iterator<String> ite = al.iterator();
while (ite.hasNext()) {
//取出当前数据
String item = ite.next();
if (item.equals("cc")) {
ite.remove();//会删除 迭代器上一次调用next获取的数据
}
}
System.out.println(al);