目录
Arrays
-
作用
-
用于操作数组
-
-
常用方法
-
把数组内容转换为一个字符串
-
String static toString(数组)
-
-
数组排序的方法
-
基本数据类型的数组
-
static void sort(数组)
-
-
引用数据类型的数组
-
static void sort(数组)
-
JavaBean实现Comparable<泛型>
-
重写一个 compareTo( 类型 对象名), 指定比较规则
-
-
static void sort(数组 , new Comparator<泛型>(){ 重写规则 })
-
-
-
数组元素查找的方法
-
int binarySearch(数组, 元素)
-
-
import java.util.Arrays;
import java.util.function.IntUnaryOperator;
public class Test {
public static void main(String[] args) {
int[] arr = {3,2,1,4,5};
System.out.println(Arrays.toString(arr));//转为字符串输出
int[] arr1 = Arrays.copyOfRange(arr,0,2);
System.out.println(Arrays.toString(arr1));//拷贝索引0-2的内容到新数组(不包括2)
int[] arr2 = Arrays.copyOf(arr,4);
System.out.println(Arrays.toString(arr2));//拷贝4个元素到新数组
Arrays.setAll(arr, new IntUnaryOperator() {
@Override
public int applyAsInt(int operand) {
return arr[operand] += 5;
}//所有数组元素加上5
});
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
int num = Arrays.binarySearch(arr, 2);//查询数组中是否存在该元素,是返回0,否返回-1
System.out.println(num);
}
}
Lambda【重点】
-
作用
-
简化匿名内部类
-
函数式接口
-
这个接口中有且只有一个抽象方法
-
@FunctionalInterface
-
-
-
-
格式
-
(参数 ) -> { 重写的方法体 }
-
public class Student implements Comparable<Student>{//实现Comparable接口并重写compareTo方法
private String name;
private int age;
private double height;
public Student() {
}
public Student(String name, double height, int age) {
this.name = name;
this.age = age;
this.height = height;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return height
*/
public double getHeight() {
return height;
}
/**
* 设置
* @param height
*/
public void setHeight(double height) {
this.height = height;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", height=" + height +
'}';
}
@Override
public int compareTo(Student o) {
return 0;
}
}
///
import java.util.Arrays;
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
Student[] students = new Student[4];
students[0] = new Student("蜘蛛精", 169.5, 23);
students[1] = new Student("青霞", 163.8, 25);
students[2] = new Student("紫霞", 163.8, 26);
students[3] = new Student("至尊宝", 167.5, 24);
/*Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
int num = Double.compare(o1.getHeight(), o2.getHeight());
if (num == 0){
num = Double.compare(o2.getAge(), o1.getAge());
}
return num;
}
});*/
Arrays.sort(students, (Student o1,Student o2) -> {//使用Lambda简化代码
int num = Double.compare(o1.getHeight(), o2.getHeight());//比较大小,左大返回正整数,右大返回负整数
if (num == 0){
num = Double.compare(o2.getAge(), o1.getAge());
}
return num;
});
System.out.println(Arrays.toString(students));
}
}
方法引用
-
作用
-
简化Lambda表达式
-
我们写的lambda里面如果只有一句代码
-
-
-
格式
-
静态方法引用
-
类名::方法名;
-
-
成员方法引用
-
对象名::方法名;
-
-
特定类型方法引用
-
类型::方法名;
-
方法上一定是有多个参数,然后第一个参数,作为对象去调用实现的方法,后面的参数作为入参
-
-
-
构造器引用
-
类名::new
-
-
public class MyUtils {//工具类
// 静态方法,用于获取两个整数的最大值
public static int getMax(int a, int b) {
return a > b ? a : b;
}
// 实例方法,获取两个数字的最小值
public int getMin(int a, int b) {
return a < b ? a : b;
}
}
///
public class Test {
public static void main(String[] args) {
A a = MyUtils::getMax;//静态方法引用
System.out.println(a.max(10,20));
MyUtils myUtils = new MyUtils();
B b = myUtils::getMin;//成员方法引用
System.out.println(b.min(10,20));
}
}
interface A{
int max(int a, int b);
}
interface B{
int min(int a, int b);
}
算法
-
学习方式
-
掌握思想
-
再写代码实现
-
-
分类
-
冒泡排序:每次从数组中找出最大值放在数组的后面去
-
选择排序:每轮选择当前位置,开始找出后面的较小值与该位置交换
-
二分查找:每次排除一半的数据,查询数据的性能明显提高极多!二分查找正常的折半条件应该是开始位置left <= 结束位置right
-
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] arr = {3,9,6,1,5,7};
//冒泡排序
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;
}
}
}
System.out.println(Arrays.toString(arr));
int[] arr1 = {3,9,6,1,5,7};
//选择排序
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1.length; j++) {
if (arr1[i] < arr1[j]) {
int temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}
System.out.println(Arrays.toString(arr1));
int [] arr2 = {11,12,25,34,45,58,67,78,84,99};
System.out.println(get(arr2 ,100));
}
//二分查找
public static int get(int[] arr , int key) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (key > arr[mid]) {
left = mid + 1;
}else if (key < arr[mid]){
right = mid - 1;
}else {
return mid;
}
}
return -1;
}
}
正则表达式
-
作用
-
判断数据是否符合某一种规则
-
从文本中爬取数据
-
-
规则
-
[任意内容]
-
代表一个符号
-
-
\d
-
代表数字
-
-
\w
-
数字、字母、下划线
-
-
.
-
任意的符号,除了 \n
-
-
数量词
-
{1 ,}
-
至少出现1次,或多次
-
-
{1,3}
-
至少出现1次,最多出现3次
-
-
{5}
-
只能出现5次
-
-
-
()
-
分组
-
-
|
-
或者
-
-
示例
public class Test {
public static void main(String[] args) {
System.out.println("-----------------------1、字符类(只能匹配单个字符)--------------------------------");
// 1、字符类(只能匹配单个字符)
System.out.println("a".matches("[abc]")); // true
System.out.println("e".matches("[abcd]")); // false
System.out.println("d".matches("[^abc]")); // true
System.out.println("a".matches("[^abc]")); // false
System.out.println("b".matches("[a-zA-Z]")); // true
System.out.println("2".matches("[a-zA-Z]")); // false
System.out.println("k".matches("[a-z&&[^bc]]")); // true
System.out.println("b".matches("[a-z&&[^bc]]")); // false
System.out.println("ab".matches("[a-zA-Z0-9]")); // false
System.out.println("------------------ 2、.代表所有的字符-------------------------------------");
// 2、预定义字符(只能匹配单个字符) . \d \D \s \S \w \W
System.out.println("木".matches(".")); // true
System.out.println("a".matches(".")); // true
System.out.println("沐沐".matches(".")); // false
System.out.println("------------------ \\d 预定义字符[0-9]代表数字-------------------------------------");
// \d 预定义字符[0-9]代表数字
// \是表示特殊字符的: \n换行字符 \t是空格字符
// 在开发中,如果希望后面的内容就是其原始内容,应该再加上一个\转义
System.out.println("2".matches("\\d")); // true
System.out.println("21".matches("\\d")); // false
System.out.println("a".matches("\\d")); // false
System.out.println("---------------------\\D不能是数字----------------------------------");
// \D不能是数字
System.out.println("2".matches("\\D")); // false
System.out.println("a".matches("\\D")); // true
System.out.println("---------------------\\s代表空格----------------------------------");
// \s代表空格
System.out.println(" ".matches("\\s")); // true
System.out.println("a".matches("\\s")); // false
System.out.println("-----------------------\\S不能是空格--------------------------------");
// \S不能是空格
System.out.println(" ".matches("\\S")); // false
System.out.println("a".matches("\\S")); // true
System.out.println("---------------------\\w 代表[a-zA-Z_0-9]----------------------------------");
// \w 代表[a-zA-Z_0-9]
System.out.println("a".matches("\\w")); // true
System.out.println("_".matches("\\w")); // true
System.out.println("2".matches("\\w")); // true
System.out.println("ab2".matches("\\w")); // false
System.out.println("------------------\\W 不能是 [a-zA-Z_0-9]------------------------------------");
// \W 不能是 [a-zA-Z_0-9]
System.out.println("a".matches("\\W")); // false
System.out.println("2".matches("\\W")); // false
System.out.println("中".matches("\\W")); // true
System.out.println("-----------------3、 ? 代表前面的内容可以出现 1次或者0次--------------------------------------");
// 3、数量词: ? * + {n} {n, } {n, m}
// ? 代表前面的内容可以出现 1次或者0次
System.out.println("a".matches("\\w?")); // true
System.out.println("".matches("\\w?")); // true
System.out.println("abc".matches("\\w?")); // false
System.out.println("------------------------ * 0次或者多次-------------------------------");
// * 0次或者多次 w[0-9a-zA-Z_]
System.out.println("abc12".matches("\\w*")); // true
System.out.println("".matches("\\w*")); // true
System.out.println("abc12张".matches("\\w*")); // false
System.out.println("----------------------+ 1次或者多次---------------------------------");
// + 1次或者多次
System.out.println("abc12".matches("\\w+")); // true
System.out.println("".matches("\\w+")); // false
System.out.println("abc12张".matches("\\w+")); // false
System.out.println("----------------------{n} 正好n次 {n, m} 至少n次最多m次 {n, } 至少n次---------------------------------");
// {n} 正好n次 {n, m} 至少n次最多m次 {n, } 至少n次
System.out.println("a3c".matches("\\w{3}")); // true
System.out.println("abcd".matches("\\w{3}")); // false
System.out.println("abcd".matches("\\w{3,}")); // true
System.out.println("ab".matches("\\w{3,}")); // false
System.out.println("abcde木".matches("\\w{3,}")); // false
System.out.println("abc232d".matches("\\w{3,9}")); // true
System.out.println("--------------------4、其他几个常用的符号:(?i)忽略大小写 、 或:| 、 分组:()-----------------------------------");
// 4、其他几个常用的符号:(?i)忽略大小写 、 或:| 、 分组:()
System.out.println("abc".matches("(?i)abc")); // true
System.out.println("ABC".matches("(?i)abc")); // true
System.out.println("-----------");
System.out.println("aBc".matches("a((?i)b)c")); // true
System.out.println("ABc".matches("a((?i)b)c")); // false
System.out.println("-------------------需求1:要求要么是3个小写字母,要么是3个数字。------------------------------------");
// 需求1:要求要么是3个小写字母,要么是3个数字。
System.out.println("123".matches("[a-z]{3}|\\d{3}")); // true
System.out.println("abc".matches("[a-z]{3}|\\d{3}")); // true
System.out.println("ab2".matches("[a-z]{3}|\\d{3}")); // false
System.out.println("-------------------需求2:必须是”我爱“开头,中间可以是至少一个”编程“,最后至少是1个”666“---------------------------------");
// 需求2:必须是”我爱“开头,中间可以是至少一个”编程“,最后至少是1个”666“
System.out.println("我爱编程编程666666".matches("我爱(编程)+(666)+")); // true
System.out.println("我爱编编程编程666666".matches("我爱(编程)+(666)+")); // false
System.out.println("我爱编程编程编程编程编程666666666".matches("我爱(编程)+(666)+")); // true
}
}
案例
public class Test {
public static void main(String[] args) {
System.out.println(checkTime("20200810")); // true
System.out.println(checkTime("2020-08-10")); // true
System.out.println(checkTime("2020-08-31")); // true
System.out.println(checkTime("2020-08-41")); // false
System.out.println(checkTime("2020-00-21")); // false
System.out.println("------------------------------------");
System.out.println(checkEmail("326370094@qq.com")); // true
System.out.println(checkEmail("326370094@qq.com.cn")); // true
System.out.println(checkEmail("326370094@qq1234.com.cn")); // false
System.out.println(checkEmail("326370094中@qq14.com.cn")); // false
System.out.println(checkEmail("326370094@qq14")); // false
System.out.println(checkEmail("326370094@qq14.com.com.com")); // false
System.out.println("------------------------------------");
System.out.println(checkPhone("18512516758"));
System.out.println(checkPhone("1851251675"));
System.out.println(checkPhone("010-98951256"));
System.out.println(checkPhone("010-08951256"));
System.out.println(checkPhone("01098951256"));
System.out.println(checkPhone("400-618-9090"));
System.out.println(checkPhone("400-618-90900"));
System.out.println(checkPhone("4006189090"));
System.out.println("-------------------------");
System.out.println(checkPhone("400-6189090"));
}
/*
电话:手机 + 座机 + 热线电话
手机格式: 首位为1,中间是[3-9]的数字,后面9个都是数字
座机: 前面三个数字是区号(第一个一定是0后面2个数字可以是0-9之间的数字),
中间可以有横杠(-)也可以没有,
最后就是八个数字(第一个一定不是0)
热线电话:
前面三个数字就是400,可以有横杠,也可以没有(-),再来三个数字,再来一个横杠,可以有可以没有,最后再来4个数字
*/
public static boolean checkPhone(String number){
String regex = "(1[3-9]\\d{9})|(0\\d{2}-?[1-9]\\d{7})|(400-\\d{3}-\\d{4})|(400\\d{3}\\d{4})";
return number.matches(regex);
}
/*
邮箱:
可以是字母可以出现多个 加上一个@符号,可以是数字,
可以是字母,至少有2个 加上一个.加上域名,可以是两个字母,
也可以是三个 ,可以有一段,也可以有两段
326370094@qq.com
*/
public static boolean checkEmail(String email) {
String regex = "[a-zA-Z0-9]{1,}@[a-zA-Z0-9]{2,5}(\\.[a-z]{2,3}){1,2}";
return email.matches(regex);
}
/*
时间:
20230813
2023-08-31
前面四个是数字,中间可以有横杠也可以没有,月份[以0开始,后面的数字只能是1-9 , 如果是以1开头 第二个数字只能是 0 1 2 ]
日[0开头后面只能是[1-9], 12开头后面数字就可以是[0-9] , 3[0,1]]
*/
public static boolean checkTime(String time) {
String regex = "\\d{4}-?((0[1-9])|(1[012]))-?((0[1-9])|([12]\\d)|(3[01]))";
return time.matches(regex);
}
}