目录


- public class IntergerDemo {
- public static void main(String[] args) {
- int a = 12;
- Integer b = 13;
- //将基本类型变量赋给引用类型变量,实现自动装箱
- Integer b1 = a;
- //将引用类型变量赋给基本类型变量,实现自动拆箱
- int a1 = b;
- }
- }
- public class FavorateDemo {
- public static void main(String[] args) {
- //特有功能1、默认值可以是null
- Integer a = null;
- //int a = null;会报错
-
- //功能2、基本类型数据转成字符串类型
- Integer b = 10019;
- String s = b.toString();
- System.out.println(s);
-
- String s1 = Integer.toString(b);
- System.out.println(s1);
- //一般可以直接用整数加一个字符串直接转成字符串
- System.out.println(b + "");
-
- //功能3、字符串数字转换为基本类型数字,两种方法
- String s2 = "23242";
- //int i = Integer.parseInt(s2);
- int i = Integer.valueOf(s2);//更优雅
-
- }
- }
API : Pattern
- public class RegexDemo1 {
- public static void main(String[] args) {
- //校验qq号,全是数字,6-20位
- String s = "123456";
- System.out.println(checkout(s));
- System.out.println(chexkout2(s));
- }
-
-
- //使用正则表达式判断
- public static boolean chexkout2(String s){
- //6-20位数字就返回true
- return s != null && s.matches("\\d{6,20}");
- }
-
-
- public static Boolean checkout(String s){
- //学习条件判断筛选的规范格式
- //先将极端情况输出
- if(s == null || s.length() < 6 || s.length() > 20){
- return false;
- }
-
- //剩下的条件再进行细分
- for (int i = 0; i < s.length(); i++) {
- char a = s.charAt(i);
- if (a < '0' || a > '9'){
- return false;
- }
- }
- return true;
- }
- }

\d:代表数字、\w:代表英文、数字、下划线
- public class Demo {
- public static void main(String[] args) {
- System.out.println("242424ds".matches("\\w{6,}"));
-
- //正则表达式验证码校验
- //a-z、A-Z、0-9 去四位
- System.out.println("34df".matches("[a-zA-Z0-9]{4}"));
- //取字母数字下划线,但是不包括下划线。
- System.out.println("er56".matches("[\\w&&[^_]]"));
- }
- }
- public static Boolean checkPhone(String s){
- //校验手机号
- //第一位1、第二位3-9、后面9位任意
- return s.matches("1[3-9]\\d{9}");
- }
- public static Boolean checkemali(String s) {
- //校验邮箱
- //分成多个部分来表示 1811176356@qq.com 121332fsds@163.com sad232232@pci.com.cn
- //分三部分 @前部、@、@后部
- // \.告诉这个点它只是一个点 \\告诉这个\它只是一个\ \\.多层嵌套
- return s.matches("\\w{1,12}@[a-zA-Z0-9]{2,4}(\\.[a-zA-Z0-9]{2,4}){1,2}");
- }

- String name = "就安排Daaadac34_小贾cfaff564西欧a";
- // w+至少出现一次就进行分割
- String[] str = name.split("\\w+");
- for (int i = 0; i < str.length; i++) {
- System.out.println(str[i]);
- }
-
- //替换
- String str2 = name.replaceAll("\\w+"," ");
- System.out.println(str2);
- }
- public static void main(String[] args) {
- String s = "来黑马学习,电话020-43422424,或者联系邮箱" +
- "itcast@itcast.cn,电话18762832234,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(s);
-
- //4、开始爬取
- while (matcher.find()){
- String s1 = matcher.group();
- System.out.println(s1);
- }
- }

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

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

- public static int binarySearch(int[] arr,int a){
- //对数组进行排序
- Arrays.sort(arr);
- //设置前后位置
- int left = 0;
- int right = arr.length - 1;
-
- //开始折半循环
- while (left <= right){
- //取中间索引
- int middleIndex = (left + right) / 2;
- //如果中间值比a大,折右边,将右边赋值为中间索引-1
- //如果中间值比a小,折左边,将左边赋值为中间索引+1
- //直到这个中间值等于a
- if (arr[middleIndex] > a){
- right = middleIndex - 1;
- }else if (arr[middleIndex] < a){
- left = middleIndex + 1;
- }else {
- return middleIndex;
- }
- }
- return -1;
- }

- //降序排序,自定义排序只能支持引用类型
- Integer[] a = {11,22,34,32,11,100,10};
- //匿名内部类
- Arrays.sort(a, new Comparator<Integer>() {
- //参数一:被排序的数组;参数二:匿名内部类对象,代表比较器对象
- @Override
- public int compare(Integer o1, Integer o2) {
- return 0;
- }
- });
- Student[] students = new Student[3];
- students[0] = new Student("小组",23,175);
- students[1] = new Student("小红",27,185);
- students[2] = new Student("小蓝",13,165);
-
- System.out.println(Arrays.toString(students));
- Arrays.sort(students, new Comparator<Student>() {
- @Override
- //默认o2>o1
- public int compare(Student o1, Student o2) {
- //自己制定比较规则
- //按照年龄升序 o1-o2 负整数升序
- // return o1.getAge() - o2.getAge();
- //降序 o2-o1 正整数降序
- return o2.getAge() - o1.getAge();
- //小数不能直接作差 170.3-170=0.3 = 0;
- //Double.compare()比较小数,前面小于后面返回-1
- // return Double.compare(o1.getHeight(),o2.getHeight());
- }
- });
- System.out.println(Arrays.toString(students));
- }