• JavaSE基础加强、常用类补充


    目录

    一、包装类

     包装类自动拆/装箱

    二、正则表达式

    1、正则表达式初体验

     2、正则表达式的匹配规则

     正则表达式做验证码校验

    3、正则表达式的常见案例

    4、正则表达式在字符串方法中的使用

    5、正则表达式爬取内容

     三、Arrays类

    打印数组内容

    二分搜索原理

    Arrays自定义排序 Comparator接口

     自定义排序案例(浮点数不能直接做差比较)


     

    一、包装类

    • 就是八种基本数据类型的引用类型

     包装类自动拆/装箱

    1. public class IntergerDemo {
    2. public static void main(String[] args) {
    3. int a = 12;
    4. Integer b = 13;
    5. //将基本类型变量赋给引用类型变量,实现自动装箱
    6. Integer b1 = a;
    7. //将引用类型变量赋给基本类型变量,实现自动拆箱
    8. int a1 = b;
    9. }
    10. }
    1. public class FavorateDemo {
    2. public static void main(String[] args) {
    3. //特有功能1、默认值可以是null
    4. Integer a = null;
    5. //int a = null;会报错
    6. //功能2、基本类型数据转成字符串类型
    7. Integer b = 10019;
    8. String s = b.toString();
    9. System.out.println(s);
    10. String s1 = Integer.toString(b);
    11. System.out.println(s1);
    12. //一般可以直接用整数加一个字符串直接转成字符串
    13. System.out.println(b + "");
    14. //功能3、字符串数字转换为基本类型数字,两种方法
    15. String s2 = "23242";
    16. //int i = Integer.parseInt(s2);
    17. int i = Integer.valueOf(s2);//更优雅
    18. }
    19. }

    二、正则表达式

    • 正则表达式可以用一些规定的字符来制定规则,并用来校验数据格式的合法性

    API : Pattern

    1、正则表达式初体验

    1. public class RegexDemo1 {
    2. public static void main(String[] args) {
    3. //校验qq号,全是数字,6-20位
    4. String s = "123456";
    5. System.out.println(checkout(s));
    6. System.out.println(chexkout2(s));
    7. }
    8. //使用正则表达式判断
    9. public static boolean chexkout2(String s){
    10. //6-20位数字就返回true
    11. return s != null && s.matches("\\d{6,20}");
    12. }
    13. public static Boolean checkout(String s){
    14. //学习条件判断筛选的规范格式
    15. //先将极端情况输出
    16. if(s == null || s.length() < 6 || s.length() > 20){
    17. return false;
    18. }
    19. //剩下的条件再进行细分
    20. for (int i = 0; i < s.length(); i++) {
    21. char a = s.charAt(i);
    22. if (a < '0' || a > '9'){
    23. return false;
    24. }
    25. }
    26. return true;
    27. }
    28. }

     2、正则表达式的匹配规则

     \d:代表数字、\w:代表英文、数字、下划线

     正则表达式做验证码校验

    1. public class Demo {
    2. public static void main(String[] args) {
    3. System.out.println("242424ds".matches("\\w{6,}"));
    4. //正则表达式验证码校验
    5. //a-z、A-Z、0-9 去四位
    6. System.out.println("34df".matches("[a-zA-Z0-9]{4}"));
    7. //取字母数字下划线,但是不包括下划线。
    8. System.out.println("er56".matches("[\\w&&[^_]]"));
    9. }
    10. }

    3、正则表达式的常见案例

    1. public static Boolean checkPhone(String s){
    2. //校验手机号
    3. //第一位1、第二位3-9、后面9位任意
    4. return s.matches("1[3-9]\\d{9}");
    5. }
    6. public static Boolean checkemali(String s) {
    7. //校验邮箱
    8. //分成多个部分来表示 1811176356@qq.com 121332fsds@163.com sad232232@pci.com.cn
    9. //分三部分 @前部、@、@后部
    10. // \.告诉这个点它只是一个点 \\告诉这个\它只是一个\ \\.多层嵌套
    11. return s.matches("\\w{1,12}@[a-zA-Z0-9]{2,4}(\\.[a-zA-Z0-9]{2,4}){1,2}");
    12. }

    4、正则表达式在字符串方法中的使用

    1. String name = "就安排Daaadac34_小贾cfaff564西欧a";
    2. // w+至少出现一次就进行分割
    3. String[] str = name.split("\\w+");
    4. for (int i = 0; i < str.length; i++) {
    5. System.out.println(str[i]);
    6. }
    7. //替换
    8. String str2 = name.replaceAll("\\w+"," ");
    9. System.out.println(str2);
    10. }

    5、正则表达式爬取内容

    1. public static void main(String[] args) {
    2. String s = "来黑马学习,电话020-43422424,或者联系邮箱" +
    3. "itcast@itcast.cn,电话18762832234,0203232323" +
    4. "邮箱bozai@itcast.cn,400-100-3233,4001003232";
    5. //从以上内容爬出电话号码、邮箱
    6. //1、定义爬取规则,字符串形式
    7. String regex = "(\\w{1,30}@[a-zA-Z0-9]{2,20}(\\.[a-zA-Z0-9]{2,20}){1,2})|1[3-9]\\d{9}" +
    8. "|(0\\d{2,6}-?\\d{5,20})|(400-?\\d{3,9}-?\\d{3,9})";
    9. //2、把爬取规则编译成匹配对象
    10. Pattern pattern = Pattern.compile(regex);
    11. //3、得到一个内容匹配器对象
    12. Matcher matcher = pattern.matcher(s);
    13. //4、开始爬取
    14. while (matcher.find()){
    15. String s1 = matcher.group();
    16. System.out.println(s1);
    17. }
    18. }

     三、Arrays类

    • 数组操作的工具类,专门操作数组

    打印数组内容

    1. public static void main(String[] args) {
    2. int[] a = {11,99,33,44,77,66};
    3. //1、打印数组内容
    4. System.out.println(Arrays.toString(a));
    5. //2、对数组元素默认升序排序
    6. Arrays.sort(a);
    7. System.out.println(Arrays.toString(a));
    8. //3、二分搜索(前提是数组必须排好序,否则出bug)
    9. //折半查找,先找中间元素,如果比他大,就将后面小的元素直接折去,再进行折半,直到接近找到为止
    10. //Arrays提供了二分查找的API,返回元素的索引,没有返回负数 -(应该插入位置 + 1)
    11. int index = Arrays.binarySearch(a,66);
    12. System.out.println(index);
    13. }

    二分搜索原理

     通过数组总长度,先找出中间位置,当前为4,再判断索引为4元素的大小,如果小于查找的元素,则将左边部分折去,剩下索引为4~9,再对4~9进行折中,判断中间位置大小,直到接近要查找的元素,最后找到该元素的索引并返回。

    1. public static int binarySearch(int[] arr,int a){
    2. //对数组进行排序
    3. Arrays.sort(arr);
    4. //设置前后位置
    5. int left = 0;
    6. int right = arr.length - 1;
    7. //开始折半循环
    8. while (left <= right){
    9. //取中间索引
    10. int middleIndex = (left + right) / 2;
    11. //如果中间值比a大,折右边,将右边赋值为中间索引-1
    12. //如果中间值比a小,折左边,将左边赋值为中间索引+1
    13. //直到这个中间值等于a
    14. if (arr[middleIndex] > a){
    15. right = middleIndex - 1;
    16. }else if (arr[middleIndex] < a){
    17. left = middleIndex + 1;
    18. }else {
    19. return middleIndex;
    20. }
    21. }
    22. return -1;
    23. }

    Arrays自定义排序 Comparator接口

    1. //降序排序,自定义排序只能支持引用类型
    2. Integer[] a = {11,22,34,32,11,100,10};
    3. //匿名内部类
    4. Arrays.sort(a, new Comparator<Integer>() {
    5. //参数一:被排序的数组;参数二:匿名内部类对象,代表比较器对象
    6. @Override
    7. public int compare(Integer o1, Integer o2) {
    8. return 0;
    9. }
    10. });

     自定义排序案例(浮点数不能直接做差比较)

    1. Student[] students = new Student[3];
    2. students[0] = new Student("小组",23,175);
    3. students[1] = new Student("小红",27,185);
    4. students[2] = new Student("小蓝",13,165);
    5. System.out.println(Arrays.toString(students));
    6. Arrays.sort(students, new Comparator<Student>() {
    7. @Override
    8. //默认o2>o1
    9. public int compare(Student o1, Student o2) {
    10. //自己制定比较规则
    11. //按照年龄升序 o1-o2 负整数升序
    12. // return o1.getAge() - o2.getAge();
    13. //降序 o2-o1 正整数降序
    14. return o2.getAge() - o1.getAge();
    15. //小数不能直接作差 170.3-170=0.3 = 0;
    16. //Double.compare()比较小数,前面小于后面返回-1
    17. // return Double.compare(o1.getHeight(),o2.getHeight());
    18. }
    19. });
    20. System.out.println(Arrays.toString(students));
    21. }

  • 相关阅读:
    ubuntu|23 安装Gnome主题
    web网页设计实例作业HTML+CSS+JavaScript蔬菜水果商城购物设计
    【Node.js入门】1.2 部署Node.js开发环境
    day18学习总结
    网络并发编程
    c++——内存管理
    抖音步骤计划设置|成都聚华祥
    HTML5基础
    ROS与QT的那些事--0 该教程安排及后续安排
    MySQL索引原理和实现
  • 原文地址:https://blog.csdn.net/m0_56044262/article/details/125574347