异常:https://blog.csdn.net/m0_60388241/article/details/124668851?spm=1001.2014.3001.5501
自定义异常:
继承RuntimeException:运行时异常
继承Exception:编译器异常
Lambda表达式
作用
简化匿名内部类代码的书写
使用前提
Lambda表达式只能简化函数式接口的匿名内部类!!!
什么是函数式接口?
1.有且仅有一个抽象方法的接口
2.大部分函数式接口上面都会有一个@FunctionalInterface的注解,有该注解的接口就必定是函数式接口 Lambda格式 (被重写方法的参数列表)->{ 被重写方法的方法体; }
Lambda格式:
(被重写方法的参数列表)->{
被重写方法的方法体;
}
public class Demo2 {
public static void main(String[] args) {
//调用test,最后打印乌鸦喝水
test(()-> System.out.println("乌鸦喝水"));
test(new Bird() {
@Override
public void drink() {
System.out.println("乌鸦喝水");
}
});
}
public static void test(Bird bird) {
bird.drink();
}
}
interface Bird {
//无参数无返回值
void drink();
}
public class Demo3 {
public static void main(String[] args) {
//调用test方法, 最后显示 哈哈哈哈
test( info-> System.out.println(info));
}
public static void test(Printer printer) {
printer.fun("哈哈哈哈");
}
}
interface Printer {
void fun(String info);
}
public class Demo4 {
public static void main(String[] args) {
//调用test完成 a+b 的计算
test(( a , b )-> a+b);
}
public static void test(Calculator calculator) {
System.out.println("运算结果:" + calculator.add(10, 20));
}
}
interface Calculator {
int add(int a, int b);
}
public class Demo3 {
public static void main(String[] args) {
//调用test方法, 最后显示 哈哈哈哈
test( info-> System.out.println(info));
}
public static void test(Printer printer) {
printer.fun("哈哈哈哈");
}
}
interface Printer {
void fun(String info);
}
方法引用
Arrays.sort(students ,CompareByData :: compareByAge1);
Arrays.sort(students ,new CompareByData() :: compareByAge2);
class CompareByData {
//静态方法实现升序
public static int compareByAge1(Student s1, Student s2) {
return s1.getAge() - s2.getAge();
}
//实例方法实现升序
public int compareByAge2(Student s1, Student s2) {
return s1.getAge() - s2.getAge();
}
}
特定类型方法的引用
如果某个Lambda只是调用一个实例方法,并且前面参数列表中第一个参数作为主调,后面的参数作为入参,即可使用 类型::方法
public class Demo2 {
public static void main(String[] args) {
String[] names = {"boby", "angela", "Andy", "dlei", "caocao", "Babo", "jack", "Cici"};
//1、使用匿名内部类对数组进行排序,规则是忽略大小写进行排序(String int compareToIgnoreCase(String str))
Arrays.sort(names, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.compareToIgnoreCase(o2);
}
});
//2、 Lambda简化上述代码
Arrays.sort(names, (o1 , o2)-> o1.compareToIgnoreCase(o2));
//3、使用特定类型方法的引用 简化Lambda代码
Arrays.sort(names, String :: compareToIgnoreCase);
//打印结果
System.out.println(Arrays.toString(names));
}
}
构造器的引用
如果某个Lambda表达式里只是在创建对象,并且前后参数情况一致,就可以使用构造器引用 类名::new
public class Demo3 {
public static void main(String[] args) {
CreateCar createCar = null;
//1. 使用匿名内部类方式调用CreateCar实现对象的创建
// createCar = new CreateCar() {
// @Override
// public Car create(String brand, double price) {
// return new Car(brand, price);
// }
// };
//2. 使用Lambda优化上面代码
//createCar = (brand, price)-> new Car(brand, price);
//3. 构造器引用 类名::new
createCar = Car::new;
//对象打印
System.out.println(createCar.create("宝马", 100000));
}
}
//接口功能: 完成Car对象的创建
interface CreateCar {
Car create(String brand, double price);
}
class Car {
private String brand;
private double price;
public Car(String brand, double price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
常见算法
冒泡排序
每次比较两个相邻的元素,小的放左边大的放右边
public class Demo1 {
public static void main(String[] args) {
int[] arr = {5, 2, 3, 1};
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void bubbleSort(int[] arr) {
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;
}
}
}
}
}
选择排序
每轮选择当前位置,开始找后面的最小值,与当前位置交换
public class Demo2 {
public static void main(String[] args) {
int[] arr = {5, 2, 3, 1};
selectionSort(arr);
System.out.println(Arrays.toString(arr));
}
private static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j <= arr.length - 1; j++) {
if (arr[i] > arr[j]){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
}
二分查找算法
前提是数组有序 特点是每一次查找完成,会舍去一半元素
public class Demo1 {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println(binarySearch(arr, 1));
System.out.println(binarySearch(arr, 2));
System.out.println(binarySearch(arr, 3));
System.out.println(binarySearch(arr, 4));
System.out.println(binarySearch(arr, 5));
System.out.println(binarySearch(arr, 6));
System.out.println(binarySearch(arr, 7));
System.out.println(binarySearch(arr, 8));
System.out.println(binarySearch(arr, 9));
System.out.println(binarySearch(arr, 10));
}
//二分查找
private static int binarySearch(int[] arr, int num) {
int left = 0;
int right = arr.length - 1;
while (left <= right){
int mid = (left + right) / 2;
if(num == arr[mid] ){
return mid;
}else if( num > arr[mid]){
left = mid + 1;
}else if (num < arr[mid]){
right = mid - 1;
}
}
return -1;
}
}
正则表达式
作用一:用来校验数据格式是否合法
作用二:在一段文本中查找满足要求的内容
作用三:将文本中的身份证号进行替换