目录
第10章 API与常见算法
10.1 和数学相关的类
10.1.1 java.lang.Math
10.1.2 java.math包
(1)BigInteger
(2)BigDecimal
(3)RoundingMode枚举类
10.1.3 java.util.Random
10.2 日期时间API
10.2.1 JDK1.8之前
1、java.util.Date
2、java.text.SimpleDateFormat
3、java.util.TimeZone
4、java.util.Locale
5、java.util.Calendar
10.2.2 JDK1.8之后
1、本地日期时间:LocalDate、LocalTime、LocalDateTime
2、指定时区日期时间:ZondId和ZonedDateTime
3、持续日期/时间:Period和Duration
4、DateTimeFormatter:日期时间格式化
10.3 系统相关类
10.3.1 java.lang.System类
10.3.3 java.lang.Runtime类
10.4 数组工具类
10.4.1 java.util.Arrays类
10.4.2 java.lang.System类
10.4.3 数组的算法升华
1、数组的扩容
2、删除数组[index]位置的元素
3、数组[index]位置插入新元素
java.lang.Math
类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。类似这样的工具类,其所有方法均为静态方法,并且不会创建对象,调用起来非常简单。
public static double abs(double a) :返回 double 值的绝对值。
double d1 = Math.abs(-5); //d1的值为5
double d2 = Math.abs(5); //d2的值为5
public static double ceil(double a) :返回大于等于参数的最小的整数。
double d1 = Math.ceil(3.3); //d1的值为 4.0
double d2 = Math.ceil(-3.3); //d2的值为 -3.0
double d3 = Math.ceil(5.1); //d3的值为 6.0
public static double floor(double a) :返回小于等于参数最大的整数。
double d1 = Math.floor(3.3); //d1的值为3.0
double d2 = Math.floor(-3.3); //d2的值为-4.0
double d3 = Math.floor(5.1); //d3的值为 5.0
public static long round(double a) :返回最接近参数的 long。(相当于四舍五入方法)
long d1 = Math.round(5.5); //d1的值为6.0
long d2 = Math.round(5.4); //d2的值为5.0
public static double pow(double a,double b):返回a的b幂次方法
public static double sqrt(double a):返回a的平方根
public static double random():返回[0,1)的随机值
public static final double PI:返回圆周率
public static double max(double x, double y):返回x,y中的最大值
public static double min(double x, double y):返回x,y中的最小值
double result = Math.pow(2,31);
double sqrt = Math.sqrt(256);
double rand = Math.random();
double pi = Math.PI;
BigInteger
用于表示任意大小的整数。由于 Java 中的基本数据类型(如 int
, long
)有固定的范围和大小,因此当需要处理超出这些范围的大整数时,BigInteger
就变得非常有用。
BigInteger(String val)
BigInteger add(BigInteger val)
BigInteger subtract(BigInteger val)
BigInteger multiply(BigInteger val)
BigInteger divide(BigInteger val)
BigInteger remainder(BigInteger val)
....
public void test01(){
// long bigNum = 123456789123456789123456789L;
BigInteger b1 = new BigInteger("123456789123456789123456789");
BigInteger b2 = new BigInteger("78923456789123456789123456789");
// System.out.println("和:" + (b1+b2));//错误的,无法直接使用+进行求和
System.out.println("和:" + b1.add(b2));
System.out.println("减:" + b1.subtract(b2));
System.out.println("乘:" + b1.multiply(b2));
System.out.println("除:" + b2.divide(b1));
System.out.println("余:" + b2.remainder(b1));
}
BigDecimal
用于处理任意精度的十进制数。提供了任意精度的有符号十进制数字的表示,以及对这些数字进行算术运算的方法。
BigDecimal(String val)
BigDecimal add(BigDecimal val)
BigDecimal subtract(BigDecimal val)
BigDecimal multiply(BigDecimal val)
BigDecimal divide(BigDecimal val)
BigDecimal divide(BigDecimal divisor, int roundingMode)
BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
BigDecimal remainder(BigDecimal val)
....
public void test02(){
/*double big = 12.123456789123456789123456789;
System.out.println("big = " + big);*/
BigDecimal b1 = new BigDecimal("123.45678912345678912345678912345678");
BigDecimal b2 = new BigDecimal("7.8923456789123456789123456789998898888");
// System.out.println("和:" + (b1+b2));//错误的,无法直接使用+进行求和
System.out.println("和:" + b1.add(b2));
System.out.println("减:" + b1.subtract(b2));
System.out.println("乘:" + b1.multiply(b2));
System.out.println("除:" + b1.divide(b2,20,RoundingMode.UP));//divide(BigDecimal divisor, int scale, int roundingMode)
System.out.println("除:" + b1.divide(b2,20,RoundingMode.DOWN));//divide(BigDecimal divisor, int scale, int roundingMode)
System.out.println("余:" + b1.remainder(b2));
}
如果项目对精度要求比较高所以使用到Bigdecimal,又涉及到范围限制,所以就涉及到如果除不尽是Bigdecimal的舍人模式的问题
CEILING :向正无限大方向舍入的舍入模式。
DOWN :向零方向舍入的舍入模式。
FLOOR:向负无限大方向舍入的舍入模式。
HALF_DOWN :向最接近数字方向舍入的舍入模式,如果与两个相邻数字的距离相等,则向下舍入。
HALF_EVEN:向最接近数字方向舍入的舍入模式,如果与两个相邻数字的距离相等,则向相邻的偶数舍入。
HALF_UP:向最接近数字方向舍入的舍入模式,如果与两个相邻数字的距离相等,则向上舍入。
UNNECESSARY:用于断言请求的操作具有精确结果的舍入模式,因此不需要舍入。 UP:远离零方向舍入的舍入模式。
用于产生随机数
boolean nextBoolean():返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 boolean 值。
void nextBytes(byte[] bytes):生成随机字节并将其置于用户提供的 byte 数组中。
double nextDouble():返回下一个伪随机数,它是取自此随机数生成器序列的、在 0.0 和 1.0 之间均匀分布的 double 值。
float nextFloat():返回下一个伪随机数,它是取自此随机数生成器序列的、在 0.0 和 1.0 之间均匀分布的 float 值。
double nextGaussian():返回下一个伪随机数,它是取自此随机数生成器序列的、呈高斯(“正态”)分布的 double 值,其平均值是 0.0,标准差是 1.0。
int nextInt():返回下一个伪随机数,它是此随机数生成器的序列中均匀分布的 int 值。
int nextInt(int n):返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值。
long nextLong():返回下一个伪随机数,它是取自此随机数生成器序列的均匀分布的 long 值。
public void test03(){
Random r = new Random();
System.out.println("随机整数:" + r.nextInt());
System.out.println("随机小数:" + r.nextDouble());
System.out.println("随机布尔值:" + r.nextBoolean());
}
new Date():当前系统时间
long getTime():返回该日期时间对象距离1970-1-1 0.0.0 0毫秒之间的毫秒值
new Date(long 毫秒):把该毫秒值换算成日期时间对象
public void test5(){
long time = Long.MAX_VALUE;
Date d = new Date(time);
System.out.println(d);
}
public void test4(){
long time = 1559807047979L;
Date d = new Date(time);
System.out.println(d);
}
public void test3(){
Date d = new Date();
long time = d.getTime();
System.out.println(time);//1559807047979
}
public void test2(){
long time = System.currentTimeMillis();
System.out.println(time);//1559806982971
//当前系统时间距离1970-1-1 0:0:0 0毫秒的时间差,毫秒为单位
}
public void test1(){
Date d = new Date();
System.out.println(d);
}
SimpleDateFormat用于日期时间的格式化。
public void test10() throws ParseException{
String str = "2019年06月06日 16时03分14秒 545毫秒 星期四 +0800";
SimpleDateFormat sf = new SimpleDateFormat("yyyy年MM月d