1:常用Api
1:Math
Math类中的方法全是静态方法,直接用类名调用即可
| 方法名 | 说明 |
| public static int abs(int a) | 获取参数a的绝对值 |
| public static double ceil(double a) | 向上取整 |
| public static double floor(double a) | 向下取整 |
| public static double pow(double a,double b) | 获取a的b次幂 |
| public static long round(double a) | 四舍五入取整 |
public class MathDemo01 {
public static void main(String[] args) {
// 1.取绝对值,返回正数
System.out.println(Math.abs(10)); // 10
System.out.println(Math.abs(-10.3)); // 10.3
// 向上取整
System.out.println(Math.ceil(4.000000001)); // 5.0
// 向下取整
System.out.println(Math.floor(4.999999)); // 4.0
// 求指数次方
System.out.println(Math.pow(2,3)); // 8.0
// 四舍五入
System.out.println(Math.round(4.4999)); // 4
}
2:System
静态方法
1: public static void exit(int status): 终止JVM虚拟机,非 0 是异常终止
2: public static long currentTimeMillis():获取当前系统此刻时间毫秒值
可以做数组的拷贝
3: arraycopy (Object var1, int var2, Object var3,int var4,int var5 )
参数一: 原数组
参数二: 从原数组的哪个索引位置开始复制
参数三:目标数组
参数四:赋值到目标数组的哪个位置
参数五:赋值几个
public class SystemDemo {
public static void main(String[] args) {
System.out.println("程序开始。。。。");
// 1.终止当前虚拟机
// System.exit(0); //0代表正常终止
// 2.得到系统当前时间毫秒值
long time = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
System.out.println(sdf.format(time));
// 3.可以做数组的拷贝(了解)
int[] arrs1 = new int[]{10,20,30,40,50,60,70};
int[] arrs2 = new int[6]; // [0,0,0,0,0,0]
// arrs2 = [0,30,40,50,0,0]
System.arraycopy(arrs1,2,arrs2,1,3);
System.out.println(Arrays.toString(arrs2));
}
}
3:toString
public class Test04ToString {
public static void main(String[] args) {
Student stu = new Student("小王", 22);
//重写toString方法前打印地址值,重写之后打印学生对象信息
System.out.println(stu);Student{name='小王', age=22}
System.out.println(stu.toString());//Student{name='小王', age=22}
}
}
class Student{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//重写toString方法
@Override
public String toString() {
return "Student{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}
4:equals
- public static boolean equals(Object a, Object b)
- 比较两个对象
- 底层进行非空判断,从而可以避免空指针异常,更安全
- public static boolean isNull(Object obj)
- 判断变量是否为 null , 为 null 返回 true
public class Test05Equals {
public static void main(String[] args) {
Student1 stu0 = new Student1("王", 22);
Student1 stu1 = new Student1("王", 22);
System.out.println(stu0.equals(stu1));//true .因为重写了equals方法,对比的就是数据内容
}
}
class Student1{
private String name;
private int age;
public Student1() {
}
public Student1(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//重写toString方法
@Override
public String toString() {
return "Student1{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
//重写equals方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student1 student1 = (Student1) o;
if (age != student1.age) return false;
return name != null ? name.equals(student1.name) : student1.name == null;
}
}
5:Objects 类
- 没有构造方法,都静态方法,直接用类名进行调用。
6:Object面试题 7:BigDecimal大数据类
- 创建对象的方式
public static BigDecimal (): 包装浮点数成为大数据对象
2.方法
public class Test09BigDecimal {
public static void main(String[] args) {
//如果进行精确的运算,就使用BigDecimal的字符串构造方法
BigDecimal b1 = new BigDecimal("0.1");
BigDecimal b2 = new BigDecimal("0.2");
//相加add
BigDecimal add = b1.add(b2);
System.out.println("和是" + add);//和是0.3
//相减subtract
BigDecimal subtract = b1.subtract(b2);
System.out.println("差为" + subtract);//差为-0.1
//相乘multiply
BigDecimal multiply = b1.multiply(b2);
System.out.println("积为" + multiply);//积为0.02
//相除devide
BigDecimal divide = b1.divide(b2);
System.out.println("商为" + divide);//商为0.5
}
}
public class Test10BigDdecimal {
public static void main(String[] args) {
//创建BigDecimal对象
BigDecimal b1 = new BigDecimal("10.0");
BigDecimal b2 = new BigDecimal("3.0");
//使用BigDecimal除法的舍入模式
//divide(参数,小数点后几位,舍入模式)
BigDecimal d1 = b1.divide(b2, 2 , BigDecimal.ROUND_UP);
System.out.println("进一法结果是" +d1);//进一法结果是3.34
BigDecimal d2 = b1.divide(b2, 2, BigDecimal.ROUND_FLOOR);
System.out.println("去尾法结果是" + d2);//去尾法结果是3.33
BigDecimal d3 = b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP);
System.out.println("四舍五入法结果是" + d3);//四舍五入法结果是3.33
}
}
8:包装类(Integer)
- Java 为了一切皆对象的思想统一,把8种基本类型转换成对应的类,这个类称为基本数据类型的包装类
- 获取包装类的方法
public class Test12Integer {
public static void main(String[] args) {
//先使用过时的构造方法
Integer i1 = new Integer(100);
Integer i2 = new Integer("100");
System.out.println(i1 + "," + i2);//100,100
//使用目前使用的构造方法
Integer i3 = Integer.valueOf(100);
Integer i4 = Integer.valueOf("100");
System.out.println(i3 + ", " + i4);//100, 100
System.out.println(i3 +i4);//200
}
}
2. 自动装箱和自动拆箱
public class PackageClass02 {
public static void main(String[] args) {
// 1.把基本数据类型的值转成字符串
Integer it = 100 ;
// a.调用toString()方法。
String itStr = it.toString();
System.out.println(itStr+1);
// 1001
// b.调用Integer.toString(基本数据类型的值)得到字符串。
String itStr1 = Integer.toString(it);
System.out.println(itStr1+1);
//1001
// c.直接把基本数据类型+空字符串就得到了字符串。
String itStr2 = it+"";
System.out.println(itStr2+1);
//1001
// 2.把字符串类型的数值转换成对应的基本数据类型的值。(真的很有用)
String numStr = "23";
//int numInt = Integer.parseInt(numStr);
int numInt = Integer.valueOf(numStr);
// Integer.valueOf 自动转int
System.out.println(numInt+1);
// 24
String doubleStr = "99.9";
//double doubleDb = Double.parseDouble(doubleStr);
double doubleDb = Double.valueOf(doubleStr);
// Double.valueOf 自动转double
System.out.println(doubleDb+0.1);
}
}
3:int 和String类型的转换
9:二分查找
public class Test17ErFenChaZhao {
public static void main(String[] args) {
int [] arr ={1,5,9,12,34,45,67,78};
int i = 67;
//调用方法获取索引
int index = getIndex(i,arr);
System.out.println(index);
}
private static int getIndex(int i,int[] arr) {
//首先定义左右两个变量
int l = 0;
int r = arr.length-1;
while (l>1;
if (arr[m] >i){
//表示要查找的元素在左边
r = m-1;
}else if (arr[m] < i){
//表示要查找的元素在右边
l = m+1;
}else {
//m索引指向的元素==i,返回索引值
return m;
}
}
return -1;
}
}
10:冒泡排序
public class Test19MaoPaoPaiXu {
public static void main(String[] args) {
int[] arr = {2,9,3,13,26,24,17};
//调用方法
//升序
maoPaoShengXu(arr);
//降序
maoPaoJiangXu1(arr);
//降序2
maoPaoJiangXu2(arr);
}
//降序3
private static void maoPaoJiangXu2(int[] arr) {
for (int i = arr.length - 1; i >= 0; i--) {
for (int j = arr.length-1;j>0;j--){
if (arr[j] >arr[j-1]){
int temp = arr[j-1];
arr[j-1] =arr[j];
arr[j] = temp;
}
}
}
for (int i1 : arr) {
System.out.print(i1 + " ");
}
}
//降序
private static void maoPaoJiangXu1(int[] arr) {
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j arr[j+1]){
int temp = arr[j +1];
arr[j+1] =arr[j];
arr[j] = temp;
}
}
}
for (int i1 = 0; i1 < arr.length; i1++) {
System.out.print(arr[i1] + " ");
}
System.out.println();
}
}
11:递归
//求5的阶乘
public class Test21DiGui {
public static void main(String[] args) {
//调用方法1
int num = getNum(5);
System.out.println(num);
}
private static int getNum(int i) {
if (i ==1){
return 1;
}else {
return i*getNum(i-1);
}
}
}
12:快排
public class Test25KuaiPai{
public static void main(String[] args) {
int[] arr = {5,8,2,4,9,7};
//调用方法
kp(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
//自定义快排的方法
private static void kp(int[] arr, int start, int end) {
//递归的出口,如果排序范围内只有一个元素或一个都没有,不需要排序了
if(start >= end){
return;
}
// 1: 由于需要为递归做准备,需要先将本次排序的老范围记录下来
int start0 = start;
int end0 = end;
//先找基准数
int jzs = arr[start];
//循环找
while (start != end){
//循环从右边找比基准数小的
while (arr[end] >= jzs && start < end){
end--;
}
//如果23行的while循环结束了,说明了什么? 说明了要么是end位置的元素比基准数小,要么就是end==start了
//然后从左边找比基准数大的
while (arr[start] <= jzs && start
13:Arrays工具类
2:Date时间类和异常
1:Date时间类
- 包: java.util.Date
- 构造器
public Date() 创建当前系统的此刻日期时间对象
public Date(long time) 把时间毫秒值转换成日期对象
- 方法
public long getTime() 返回自1970年1月1日00:00:00 以来走过的毫秒值
public class Test01Date {
public static void main(String[] args) {
Date date = new Date();
System.out.println(date);//Sun Oct 24 11:09:08 CST 2021 输出当前计算机时间
Date date1 = new Date(0L);
System.out.println(date1);//Thu Jan 01 08:00:00 CST 1970
//将日期对象转换成毫秒值
// 返回自此 Date对象表示的1970年1月1日00:00:00 GMT以来的毫秒数。
long time = date.getTime();
System.out.println(time);//1635044948949
}
}
2:SimplDateFormat 格式化类
- 一般都是使用有参构造,使用指定的日期格式
public class Test04SimpleDateFormatDemo {
public static void main(String[] args) throws ParseException {
String s = "1996/05/26";
//将s转换成Date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date d = sdf.parse(s);
System.out.println(d);//Sun May 26 00:00:00 CST 1996
System.out.println(d.toLocaleString());//1996年5月26日 上午12:00:00
System.out.println(d.getTime());//833040000000
//获取星期,再次创建一个新的日期格式化对象,格式化成带日期的格式即可
SimpleDateFormat sdf2 = new SimpleDateFormat("E");
String s1 = sdf2.format(d);
System.out.println(s + "对应的日期是:" + s1);//1996/05/26对应的日期是:周日
}
}
3:LocalDateTime
- 没有构造方法,使用下面两个方法 .now() 和 .of()
public class Test06LocalDateTime {
public static void main(String[] args) {
//获取当前的计算机系统时间
LocalDateTime now = LocalDateTime.now();
System.out.println(now);//2021-10-24T11:23:21.553468900
//获取给定的时间对象
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
System.out.println(localDateTime);//2020-11-11T11:11:11
}
}
- 调用方法
public class Test07LocalDateTime {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 20);
//public int getYear() 获取年
int year = localDateTime.getYear();
System.out.println("年为" +year);//年为2020
//public int getMonthValue() 获取月份(1-12)
int month = localDateTime.getMonthValue();
System.out.println("月份为" + month);//月份为11
Month month1 = localDateTime.getMonth();
// System.out.println(month1);
//public int getDayOfMonth() 获取月份中的第几天(1-31)
int day = localDateTime.getDayOfMonth();
System.out.println("日期为" + day);//日期为11
//public int getDayOfYear() 获取一年中的第几天(1-366)
int dayOfYear = localDateTime.getDayOfYear();
System.out.println("这是一年中的第" + dayOfYear + "天");//这是一年中的第316天
//public DayOfWeek getDayOfWeek()获取星期
int value = localDateTime.getDayOfWeek().getValue();
System.out.println("星期为" + value);//星期为3
//public int getMinute() 获取分钟
int minute = localDateTime.getMinute();
System.out.println("分钟为" + minute);//分钟为11
//public int getHour() 获取小时
int hour = localDateTime.getHour();
System.out.println("小时为" + hour);//小时为11
}
}
4:LocalDate 和 LocalTime 和格式化类 DateTimeormatter
5:plus ,minus 和 with(修改方法)
6:时间间隔对象(Period 和 Duration)
public class Test14Duration {
public static void main(String[] args) {
//1:获取出生时间对象
LocalDateTime s = LocalDateTime.of(1996, 05, 26, 05, 23, 42);
//获取当前时间对象
LocalDateTime now = LocalDateTime.now();
Duration between = Duration.between(s, now);
//获取的信息是天
long l = between.toDays();
System.out.println("小王已经在世界上存活了" + l + "天");//小王已经在世界上存活了9282天
}
}
7:异常
8:throws
9:throw
10:try catch



