目录
包装类:其实就是8种基本数据类型对应的引用类型。

为什么提供包装类?
Java为了实现一切皆对象,为8种基本类型提供了对应的引用类型。
后面的集合和泛型其实也只能支持包装类型,不支持基本数据类型。
自动装箱:基本类型的数据和变量可以直接赋值给包装类型的变量。
自动拆箱:包装类型的变量可以直接赋值给基本数据类型的变量。
包装类的特有功能
包装类的变量的默认值可以是null,容错率更高。
可以把基本类型的数据转换成字符串类型(用处不大)。
可以把字符串类型的数值转换成真实的数据类型(真的很有用),具体使用包装类型.valueOf(字符串变量)转为基本数据类型。
- /**
- 目标:明白包装类的概念,并使用。
- */
- public class Test {
- public static void main(String[] args) {
- int a = 10;
- Integer a1 = 11;
- Integer a2 = a; // 自动装箱
- System.out.println(a);
- System.out.println(a1);
-
- Integer it = 100;
- int it1 = it; // 自动拆箱
- System.out.println(it1);
-
- double db = 99.5;
- Double db2 = db; // 自动装箱了
- double db3 = db2; // 自动拆箱
- System.out.println(db3);
-
- // int age = null; // 报错了!
- Integer age1 = null;
- Integer age2 = 0;
-
- System.out.println("-----------------");
- // 1、包装类可以把基本类型的数据转换成字符串形式。(没啥用)
- Integer i3 = 23;
- String rs = i3.toString();
- System.out.println(rs + 1);
-
- String rs1 = Integer.toString(i3);
- System.out.println(rs1 + 1);
-
- // 可以直接+字符串得到字符串类型
- String rs2 = i3 + "";
- System.out.println(rs2 + 1);
-
- System.out.println("-----------------");
-
- String number = "23";
- //转换成整数
- // int age = Integer.parseInt(number);
- int age = Integer.valueOf(number);
- System.out.println(age + 1);
-
- String number1 = "99.9";
- //转换成小数
- // double score = Double.parseDouble(number1);
- double score = Double.valueOf(number1);
- System.out.println(score + 0.1);
- }
- }
需求:假如现在要求校验一个qq号码是否正确,6位及20位之内,必须全部是数字 。
- public class RegexDemo1 {
- public static void main(String[] args) {
- // 需求:校验qq号码,必须全部数字 6 - 20位
- System.out.println(checkQQ("251425998"));
- System.out.println(checkQQ("2514259a98"));
- System.out.println(checkQQ(null));
- System.out.println(checkQQ("2344"));
-
- System.out.println("-------------------------");
- // 正则表达式的初体验:
- System.out.println(checkQQ2("251425998"));
- System.out.println(checkQQ2("2514259a98"));
- System.out.println(checkQQ2(null));
- System.out.println(checkQQ2("2344"));
-
- }
-
- public static boolean checkQQ2(String qq){
- return qq != null && qq.matches("\\d{6,20}");
- }
-
-
- public static boolean checkQQ(String qq){
- // 1、判断qq号码的长度是否满足要求
- if(qq == null || qq.length() < 6 || qq.length() > 20 ) {
- return false;
- }
-
- // 2、判断qq中是否全部是数字,不是返回false
- // 251425a87
- for (int i = 0; i < qq.length(); i++) {
- // 获取每位字符
- char ch = qq.charAt(i);
- // 判断这个字符是否不是数字,不是数字直接返回false
- if(ch < '0' || ch > '9') {
- return false;
- }
- }
-
- return true; // 肯定合法了!
- }
- }
字符串对象提供了匹配正则表达式的方法:
public boolean matches(String regex): 判断是否匹配正则表达式,匹配返回true,不匹配返回false。

- /**
- 目标:全面、深入学习正则表达式的规则
- */
- public class RegexDemo02 {
- public static void main(String[] args) {
- //public boolean matches(String regex):判断是否与正则表达式匹配,匹配返回true
- // 只能是 a b c
- System.out.println("a".matches("[abc]")); // true
- System.out.println("z".matches("[abc]")); // false
-
- // 不能出现a b c
- System.out.println("a".matches("[^abc]")); // false
- System.out.println("z".matches("[^abc]")); // true
-
-
- System.out.println("a".matches("\\d")); // false
- System.out.println("3".matches("\\d")); // true
- System.out.println("333".matches("\\d")); // false
- System.out.println("z".matches("\\w")); // true
- System.out.println("2".matches("\\w")); // true
- System.out.println("21".matches("\\w")); // false
- System.out.println("你".matches("\\w")); //false
- System.out.println("你".matches("\\W")); // true
- System.out.println("---------------------------------");
- // 以上正则匹配只能校验单个字符。
-
- // 校验密码
- // 必须是数字 字母 下划线 至少 6位
- System.out.println("2442fsfsf".matches("\\w{6,}"));
- System.out.println("244f".matches("\\w{6,}"));
-
- // 验证码 必须是数字和字符 必须是4位
- System.out.println("23dF".matches("[a-zA-Z0-9]{4}"));
- System.out.println("23_F".matches("[a-zA-Z0-9]{4}"));
- System.out.println("23dF".matches("[\\w&&[^_]]{4}"));
- System.out.println("23_F".matches("[\\w&&[^_]]{4}"));
-
- }
- }
需求:
请编写程序模拟用户输入手机号码,邮箱号码,验证格式正确,并给出提示,直到格式输入正确为止。
分析:
定义方法,接收用户输入的数据,使用正则表达式完成检验,并给出提示。
- import java.util.Arrays;
- import java.util.Scanner;
-
- public class RegexTest3 {
- public static void main(String[] args) {
- // 目标:校验 手机号码 邮箱 电话号码
- // checkPhone();
- // checkEmail();
- // checkTel();
-
- int[] arr = {10, 4, 5,3, 4,6, 2};
- System.out.println(Arrays.binarySearch(arr, 2));
-
- }
-
- public static void checkTel(){
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("请您输入您的电话号码:");
- String tel = sc.next();
- // 判断邮箱格式是否正确 027-3572457 0273572457
- if(tel.matches("0\\d{2,6}-?\\d{5,20}")){
- System.out.println("格式正确,注册完成!");
- break;
- }else {
- System.out.println("格式有误!");
- }
- }
- }
-
- public static void checkEmail(){
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("请您输入您的注册邮箱:");
- String email = sc.next();
- // 判断邮箱格式是否正确 3268847878@qq.com
- // 判断邮箱格式是否正确 3268847dsda878@163.com
- // 判断邮箱格式是否正确 3268847dsda878@pci.com.cn
- if(email.matches("\\w{1,30}@[a-zA-Z0-9]{2,20}(\\.[a-zA-Z0-9]{2,20}){1,2}")){
- System.out.println("邮箱格式正确,注册完成!");
- break;
- }else {
- System.out.println("格式有误!");
- }
- }
- }
-
- public static void checkPhone(){
- Scanner sc = new Scanner(System.in);
- while (true) {
- System.out.println("请您输入您的注册手机号码:");
- String phone = sc.next();
- // 判断手机号码的格式是否正确
- if(phone.matches("1[3-9]\\d{9}")){
- System.out.println("手机号码格式正确,注册完成!");
- break;
- }else {
- System.out.println("格式有误!");
- }
- }
- }
- }
正则表达式在字符串方法中的使用

- /**
- 目标:正则表达式在方法中的应用。
- public String[] split(String regex):
- -- 按照正则表达式匹配的内容进行分割字符串,反回一个字符串数组。
- public String replaceAll(String regex,String newStr)
- -- 按照正则表达式匹配的内容进行替换
- */
- public class RegexDemo04 {
- public static void main(String[] args) {
- String names = "小路dhdfhdf342蓉儿43fdffdfbjdfaf小何";
-
- String[] arrs = names.split("\\w+");
- for (int i = 0; i < arrs.length; i++) {
- System.out.println(arrs[i]);
- }
-
- String names2 = names.replaceAll("\\w+", " ");
- System.out.println(names2);
- }
- }
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- /**
- 拓展:正则表达式爬取信息中的内容。(了解)
- */
- public class RegexDemo05 {
- public static void main(String[] args) {
- String rs = "来黑马程序学习Java,电话020-43422424,或者联系邮箱" +
- "itcast@itcast.cn,电话18762832633,0203232323" +
- "邮箱bozai@itcast.cn,400-100-3233 ,4001003232";
-
- // 需求:从上面的内容中爬取出 电话号码和邮箱。
- // 1、定义爬取规则,字符串形式
- String regex = "(\\w{1,30}@[a-zA-Z0-9]{2,20}(\\.[a-zA-Z0-9]{2,20}){1,2})|(1[3-9]\\d{9})" +
- "|(0\\d{2,6}-?\\d{5,20})|(400-?\\d{3,9}-?\\d{3,9})";
-
- // 2、把这个爬取规则编译成匹配对象。
- Pattern pattern = Pattern.compile(regex);
-
- // 3、得到一个内容匹配器对象
- Matcher matcher = pattern.matcher(rs);
-
- // 4、开始找了
- while (matcher.find()) {
- String rs1 = matcher.group();
- System.out.println(rs1);
- }
-
- }
- }
Arrays类概述:数组操作工具类,专门用于操作数组元素的。
Arrays类的常用API

- import java.util.Arrays;
-
- public class ArraysDemo1 {
- public static void main(String[] args) {
- // 目标:学会使用Arrays类的常用API ,并理解其原理
- int[] arr = {10, 2, 55, 23, 24, 100};
- System.out.println(arr);
-
- // 1、返回数组内容的 toString(数组)
- // String rs = Arrays.toString(arr);
- // System.out.println(rs);
-
- System.out.println(Arrays.toString(arr));
-
- // 2、排序的API(默认自动对数组元素进行升序排序)
- Arrays.sort(arr);
- System.out.println(Arrays.toString(arr));
-
- // 3、二分搜索技术(前提数组必须排好序才支持,否则出bug)
- int index = Arrays.binarySearch(arr, 55);
- System.out.println(index);
-
- // 返回不存在元素的规律: - (应该插入的位置索引 + 1)
- int index2 = Arrays.binarySearch(arr, 22);
- System.out.println(index2);
-
-
- // 注意:数组如果么有排好序,可能会找不到存在的元素,从而出现bug!!
- int[] arr2 = {12, 36, 34, 25 , 13, 24, 234, 100};
- System.out.println(Arrays.binarySearch(arr2 , 36));
- }
-
- }
Arrays类的排序方法

自定义排序规则
设置Comparator接口对应的比较器对象,来定制比较规则。

- import java.util.Arrays;
- import java.util.Comparator;
-
- public class ArraysDemo2 {
- public static void main(String[] args) {
- // 目标:自定义数组的排序规则:Comparator比较器对象。
- // 1、Arrays的sort方法对于有值特性的数组是默认升序排序
- int[] ages = {34, 12, 42, 23};
- Arrays.sort(ages);
- System.out.println(Arrays.toString(ages));
-
- // 2、需求:降序排序!(自定义比较器对象,只能支持引用类型的排序!!)
- Integer[] ages1 = {34, 12, 42, 23};
- /**
- 参数一:被排序的数组 必须是引用类型的元素
- 参数二:匿名内部类对象,代表了一个比较器对象。
- */
- Arrays.sort(ages1, new Comparator<Integer>() {
- @Override
- public int compare(Integer o1, Integer o2) {
- // 指定比较规则。
- // if(o1 > o2){
- // return 1;
- // }else if(o1 < o2){
- // return -1;
- // }
- // return 0;
- // return o1 - o2; // 默认升序
- return o2 - o1; // 降序
- }
- });
- System.out.println(Arrays.toString(ages1));
-
- System.out.println("-------------------------");
- Student[] students = new Student[3];
- students[0] = new Student("吴磊",23 , 175.5);
- students[1] = new Student("谢鑫",18 , 185.5);
- students[2] = new Student("王亮",20 , 195.5);
- System.out.println(Arrays.toString(students));
-
- // Arrays.sort(students); // 直接运行奔溃
- Arrays.sort(students, new Comparator<Student>() {
- @Override
- public int compare(Student o1, Student o2) {
- // 自己指定比较规则
- // return o1.getAge() - o2.getAge(); // 按照年龄升序排序!
- // return o2.getAge() - o1.getAge(); // 按照年龄降序排序!!
- // return Double.compare(o1.getHeight(), o2.getHeight()); // 比较浮点型可以这样写 升序
- return Double.compare(o2.getHeight(), o1.getHeight()); // 比较浮点型可以这样写 降序
- }
- });
- System.out.println(Arrays.toString(students));
-
-
- }
- }