【184天】黑馬程序員27天視頻學習筆記【Day14-中】

叨逼叨兩句

情緒上的緊張,讓人真的很累。java

14-(6-10):正則表達式

給定字符串數字排序

package com.test.regex;

import java.util.Arrays;

public class Demo06 {
    public static void main(String[] args) {
        /* 1. 切割字符串,獲得字符串數組
         * 2. 遍歷字符串數組,將其轉換爲int類型,並裝入int[]數組
         * 3. 對int[]數組進行排序
         * 4. 拼接int[]數組
         * 5. 輸出
         */
        String s = "91 27 46 38 50";
        String[] sArr = s.split(" ");
        int[] arr = new int[5];
        for(int i = 0; i < sArr.length; i++){
            arr[i] =Integer.parseInt(sArr[i]);
        }
        Arrays.sort(arr);
        
        //這種方法不推薦,讓內存中產生了太多垃圾
        String str = new String();
        for (int i = 0; i < arr.length; i++) {
            if(i!= arr.length - 1){
                str = str + arr[i] + " ";
            } else {
                str = str + arr[i];
            }
        }
        System.out.println(str);
        
        //這種方式推薦【還有更好的,用方法鏈】
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            if(i!=arr.length-1){
                sb.append(arr[i]+" ");
            } else {
                sb.append(arr[i]);
            }
        }
        System.out.println(sb);
    }
}

正則表達式——替換功能

  1. public String replaceAll(String regex,String replacement)正則表達式

    package com.test.regex;
    
    public class Demo07 {
        public static void main(String[] args) {
            String s = "wo111ai2222shi3333jie";
            String regex = "\\d";
            String s2 = s.replaceAll(regex,"-");
            System.out.println(s2);
        }
    }

正則表達式——分組功能

  1. 疊詞「快快樂樂」與「搞起搞起」的正則表達式編程

    package com.test.regex;
    
    public class Demo08 {
        public static void main(String[] args) {
            //疊詞「快快樂樂」「高高興興」
            String s1 = "快快樂樂";
            String s2 = "高高興興";
            String regex = "(.)\\1(.)\\2";   // \\1表明讓第一組再出現一次,\\2表明讓第二組再出現一次
            boolean a1 = s1.matches(regex);
            boolean a2 = s2.matches(regex);
            System.out.println(a1);  
            System.out.println(a2);
            
            //疊詞「搞起搞起」
            String s3 = "搞起搞起";
            String regex2 = "(..)\\1";
            boolean a3 = s3.matches(regex2);
            System.out.println(a3);
        }
    }
  2. 按照疊詞切割「sdqqfgkkkhjppppkl」數組

    package com.test.regex;
    
    public class Demo09 {
        public static void main(String[] args) {
            String s = "sdqqfgkkkhjppppkl";
            String regex = "(.)\\1+"; // + 表明出現一次到屢次【我的認爲,可想象(.)與+之間是相乘關係】
            String[] sArr = s.split(regex);
            for(int i = 0 ; i < sArr.length; i++){
                System.out.println(sArr[i]);
            }
        }
    }
  3. 將「我我....我...我.要...要要...要學....學學..學.編..編編.編.程.程.程..程」轉換爲「我要學編程」app

    package com.test.regex;
    
    public class Demo10 {
        public static void main(String[] args) {
            String s = "我我....我...我.要...要要...要學....學學..學.編..編編.編.程.程.程..程";
            String s2 = s.replaceAll("\\.", "");
            String s3 = s2.replaceAll("(.)\\1+", "$1"); //$1表明第一組中的內容
            System.out.println(s3);
            
            
            String a = "快快樂樂";
            String regex = "(.)\\1";
            String a2 = a.replaceAll(regex, "$1");
            System.out.println(a2);
        }
    }

Pattern和Matcher

典型調用順序

package com.test.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo11 {
    public static void main(String[] args) {
        //模式和匹配器的典型調用順序
        Pattern p = Pattern.compile("a*b");
        Matcher m = p.matcher("aaaaab");
        boolean b = m.matches();
        System.out.println(b);
        
        //等同於
        System.out.println("aaaaab".matches("a*b"));
        
    }
}

應用舉例:字符串中提取手機號碼

  1. 關注find()方法與group()方法的搭配運用。
  2. 區分find()方法與matches()方法。dom

    package com.test.regex;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Demo12 {
        public static void main(String[] args) {
            String s = "個人號碼:18500238888,你的號碼:13488885555,他的號碼:18752222222";
            Pattern p = Pattern.compile("1[387]\\d{9}");
            Matcher m = p.matcher(s);
            while(m.find()){       //必須find()找一次,用group()才能打印
                System.out.println(m.group());
            }        
        }
    }

14-(11-12):經常使用類

Math類

  1. public static int abs(int a):絕對值
  2. public static double ceil(double a):向上取整
  3. public static double floor(double a):向下取整
  4. public static double random()
  5. public static int round(float a)【還有參數爲double的】:四捨五入
  6. public static int max(int a,int b)【還有min】
  7. public static double pow(double a,double b):a的b次方,前面是底數,後面是指數
  8. public static double sqrt(double a):開平方

Random類

用於生成僞隨機數ui

  1. Random():空參構造生成僞隨機數,是利用當前納秒值。code

    package com.test.regex;
    
    import java.util.Random;
    
    public class Demo13 {
        public static void main(String[] args) {
                Random r = new Random();    
                for(int i = 0; i < 10; i++){
                    int x = r.nextInt();
                    System.out.println(x);
                }
        }
    }
  2. Random(long a):提供一個種子生成僞隨機數【每一次運行結果相同】排序

    package com.test.regex;
    
    import java.util.Random;
    
    public class Demo13 {
        public static void main(String[] args) {
                Random r = new Random(1000);
                int a = r.nextInt();
                int b = r.nextInt();
                System.out.println(a);
                System.out.println(b);
        }
    }
  3. nextInt():生成Int取值範圍內的僞隨機數內存

    package com.test.regex;
    
    import java.util.Random;
    
    public class Demo13 {
        public static void main(String[] args) {
            Random r = new Random();
            for(int i = 0; i < 10; i++){
                int a = r.nextInt();
                System.out.println(a);
            }
            
        }
    }
  4. nextInt(100):生成0-99的僞隨機數【要變成1-100,加1便可】

    package com.test.regex;
    
    import java.util.Random;
    
    public class Demo13 {
        public static void main(String[] args) {
            Random r = new Random();
            for(int i = 0; i < 100; i++){
                int a = r.nextInt(100);
                System.out.println(a);
            }
        }
    }
相關文章
相關標籤/搜索