1、Math
方法名 | 解释 |
---|---|
public static int abs(int a) | 返回参数的绝对值 |
public static double ceil(double a) | 向上取整 |
public static double floor(double a) | 向下取整 |
public static int round(float a) | 四舍五入 |
public static int max(int a, int b) | 获取两个实参的较大值 |
public static int min(int a, int b) | 获取两个实参的较小值 |
public static double pow(double a, double b) | 返回a的b次幂的值 |
public static double random() | 返回值为double的正值,[0.0, 1.0] |
代码演示
public class DemoMath {
public static void main(String[] args) {
int a = Math.abs(-11);
System.out.println(a); // 11
double b = Math.ceil(11.1);
System.out.println(b); // 12.0
double c = Math.floor(11.9);
System.out.println(c); // 11.0
int d = Math.round(3.14);
System.out.println(d); // 3
int e = Math.max(3, 8);
System.out.println(e); // 8
int f = Math.min(3, 8);
System.out.println(f); // 3
double g = Math.pow(2, 3);
System.out.println(g); // 8.0
double h = Math.random();
System.out.println(h); // 0.5087489913465722
}
}
2、System
方法名 | 解释 |
---|---|
public static void exit(int status) | 终止Java虚拟机的运行,0表示异常终止 |
public static long currentTimeMillis() | 以毫秒为单位返回当前时间 |
arraycopy(数据源数组, 起始索引, 目标数组, 起始索引, 拷贝个数) | 数组拷贝 |
代码演示
public class DemoSystem {
public static void mian(String[] args) {
Long l = Math.currentTimeMillis();
System.out.println(l); //打印当前时间的毫秒值
int[] arr = {1,2,3,4,5,6,7};
int[] newArr = new int[4];
// 拷贝长度为3
System.copy(arr, 0, newArr, 0, 3);
for(int i = 0; i < newArr.length; i++) {
System.out.println(newArr[i]);
}
System.out.println("正常执行");
System.exit(0); //终止程序运行
System.out.println("无法执行");
}
}
3、Object
object类是所有类的父类,只有一个空参构造
方法 | 解释 |
---|---|
public String toString | 返回对象的字符串表达形式 |
public boolean equals(比较对象) | 比较对象是否相等,默认比较地址值,重写equals() 比较内容 |
代码演示
public class Student {
private String name;
private String age;
public Student() {
}
public Student(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (name != null ? !name.equals(student.name) : student.name != null) return false;
return age != null ? age.equals(student.age) : student.age == null;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
public class DemoObject {
public static void main(String[] args) {
Student stu1 = new Student("张三","23");
Student stu2 = new Student("张三","23");
System.out.println(stu1.equals(stu2)); //true
}
}
- Object面试题
代码演示
public class DemoObject {
public static void main(String[] args) {
String s1 = "abc";
StringBuilder sb = new StringBuilder("abc");
/*
String调用的equals()方法中有instanceof判断,
如果被比较的是字符串,则比较内容
*/
System.out.println(s1.equals(sb)); //false
/*
StringBuilder类没有equals(),因此调用的还是Object的,
*/
System.out.println(sb.equals(s1)); //false
}
}
4、Objects
方法 | 解释 |
---|---|
public static String toString(对象) | 返回参数对象的字符串形式 |
public static String toString(对象, 默认字符串) | 返回参数对象的字符串形式 , 对象为null则打印默认字符串 |
public static boolean isNull(对象) | 判断对象是否为空 |
public static boolean nonNull(对象) | 判断对象是否不为空 |
代码演示
public class Student {
private String name;
private String age;
public Student() {
}
public Student(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (name != null ? !name.equals(student.name) : student.name != null) return false;
return age != null ? age.equals(student.age) : student.age == null;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}
public class DemoObjects {
public static void main(String[] args) {
Student stu1 = new Student("张三","23");
Student stu2 = null;
String s1 = Objects.toString(stu1);
String s2 = Objects.toString(stu2, "stu2为空");
// Student{name='张三', age='23'}
System.out.println(s1);
// stu2为空
System.out.println(s2);
boolean aNull = Objects.isNull(stu2);
boolean nonNull = Objects.nonNull(stu2);
System.out.println(aNull); //true
System.out.println(nonNull); //false
}
}
5、BigDecimal
java中的计算:十进制 —> 二进制(计算) —> 十进制(结果)
这种计算方式当出现小数时,会损失精度
- 构造方法:
方法名 | 解释 |
---|---|
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对象, 精确几位, 舍入模式) | 除 |
代码演示
public class DemoBigDecimal {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("10.0");
BigDecimal bd2 = new BigDecimal("3.0");
// 进一法:从精确位数后一位向前进一
BigDecimal divide1 = bd1.divide(bd2, 2, BigDecimal.ROUND_UP);
System.out.println(divide1); //3.34
// 去尾法:从精确位数向后舍去
BigDecimal divide2 = bd1.divide(bd2, 2, BigDecimal.ROUND_FLOOR);
System.out.println(divide2); //3.33
// 四舍五入
BigDecimal divide3 = bd1.divide(bd2, 2, BigDecimal.ROUND_HALF_UP);
System.out.println(divide3); //3.33
}
}
6、基本数据类型包装类
- 简而言之就是基本数据类型的对象表现形式,因为对象的操作范围更广阔。
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
- Integer构造
public static Integer valueOf(int i)
public static Integer valueOf(String s)
代码演示
public static void mian(String[] args) {
Integer i1 = Integer.valueOf(10);
Integer i2 = Integer.valueOf("100");
System.out.println(i1); // 10
System.out.println(i2); // 100
}
- 自动拆箱和自动装箱(JDK1.5以后)
装箱:把基本数据类型转换为对应的包装类类型
拆箱:把包装类类型转换为对应的基本数据类型
代码演示
public class DemoInteger {
public static void main(String[] args) {
// 基本数据类型赋值时触发装箱
Integer i1 = 99;
// 包装类类型赋值给基本数据类型时触发拆箱
int i2 = i1;
// 运算过程中也会触发拆箱
Integer i3 = 180;
i3 += 200;
// null无法进行基本数据类型转换
Integer i4 = null;
i4 += 200; //NullPointerException
}
}
- 类型转换
String 转 int:parseInt(String str)
int 转 String:valueOf(int i)
代码演示
public class DemoChange {
public static void main(String[] args) {
// String 转 int
String s1 = "123";
int i1 = Integer.parseInt(s1);
// int 转 String
int i2 = 200;
String s2 = String.valueOf(i2);
// 另一种方法:字符串拼接
}
}
7、数组的高级操作
- 二分查找
条件:二分查找的前提是顺序排序的数组
步骤:
1、定义两个变量,表示查找范围,int min = 0; int max = 最大索引
2、循环查找,min < max
3、计算 mid = (max + min ) / 2
4、判断 mid 位置的元素是否为要查找的元素,是则返回对应索引
5、如果要查找的值小于 mid 索引对应的元素值,那么 min 不变,max = mid -1, 继续循环查询
6、如果要查找的值大于 mid 索引对应的元素值,那么 max 不变,min = mid +1, 继续循环查询
7、当 min > max 时,表示查找的元素不在数组中,返回 -1
代码演示
public class DemoBinarySearch {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7};
int number = 2;
int index = myBinarySearch(arr, number);
System.out.println(index);
}
private static int myBinarySearch(int[] arr, int number) {
int min = 0;
int max = arr.length - 1;
while (min < max) {
int mid = (min + max ) / 2;
if (arr[mid] > number) {
max = mid - 1;
} else if (arr[mid] < number) {
min = mid + 1;
} else {
return mid;
}
}
return -1;
}
}
- 冒牌排序
1、相邻的元素两两相互比较,小的放左边,大的放右边
2、一次循环结束,找到最大值(不再参与比较)
3、下次循环,从剩余比较元素中寻找最大值(不再参与比较)
4、依次循环,直到最好只剩下1个值,排序结束。
代码演示
public class DemoBubbleSort {
public static void main(String[] args) {
int[] arr = {3,8,9,1,7};
// 循环,比较,获取每次比较的 max
for (int i = 0; i < arr.length - 1; i++) {
// 每次比较获取 max
for (int j = 0; j < arr.length - j; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// 数组遍历
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
- 递归
递归就是:自己调用自己
核心:出口和递归规则,没有出口将会无线调用循环
代码演示 - 递归求阶乘
public class DemoFactorial {
public static void main(String[] args) {
// 这里求10的阶乘
int result = getJC(10);
System.out.println(result); //3628800
}
private static int getJC(int i) {
// 设置出口
if (i == 1) {
return 1;
} else {
// 递归调用
return i * getJC(i - 1);
}
}
}
- 快排
定义一个基准数,从0索引开始,
从右边找比基准数小的,从左边找比基准数大的,找到后交换位置
然后左右索引向中间依次靠近查询交换,直到左右索引重合,
之后重合索引出元素与基准元素交换,一次循环结束,确定一个数的位置
然后进行折半循环排序,
代码演示
public class DemoQuiteSort {
public static void main(String[] args) {
int[] arr = {5,2,7,4,1,3,8,6};
// 0, arr.length - 1:表示排序的范围
quiteSort(arr, 0, arr.length-1);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
private static void quiteSort(int[] arr, int left, int right) {
if (right < left) {
return;
}
int left0 = left;
int right0 = right;
// 声明基准数
int baseNumber = arr[left0];
// 设置循环
while (left != right) {
//从右边开始找比基准数小的,
while (arr[right] >= baseNumber && right > left) {
right--;
}
//从左边开始找比基准数大的
while (arr[left] <= baseNumber && right > left) {
left++;
}
//交换左右找打的元素位置
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
// 基准数归位
int temp = arr[left];
arr[left] = arr[left0];
arr[left0] = temp;
quiteSort(arr, left0, left-1);
quiteSort(arr, left+1, right0);
}
}
8、Arrays
方法名 | 解释 |
---|---|
public static String toString(int[] a) | 返回指定数组内容的字符串表示形式 |
public static void sort(int[] a) | 数组元素排序 |
public static int binarySearch(int[] a, int key) | 二分查找获取元素索引 |