//獲取一個模式器
Pattern p = Pattern.compile("a*b*");//通過模式器獲取一個匹配器
Matcher m = p.matcher("aaaaab");//進行匹配boolean b = m.matches();
System.out.println(b);//true
Matcher匹配器的方法:
boolean find ()
嘗試查找與該模式匹配的輸入序列的下一個子序列。
String group ()
返回由以前匹配操作所匹配的輸入子序列。
舉例:
import java.util.regex.Matcher;import java.util.regex.Pattern;publicclassBlog3{publicstaticvoidmain(String[] args){//把下面這個字符串中是三個字母組成的單詞,獲取出來// da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?
String str="da jia ting wo shuo, jin tian yao xia yu, bu shang wan zi xi, gao xing bu?";//定義三個字母單詞的正則表達式 注意加單詞邊界,否則是三個字符的都會取到
String strRegx="\\b[a-z]{3}\\b";//把正則表達式封裝到模式器中
Pattern p = Pattern.compile(strRegx);//獲取匹配器,把待匹配的字符串傳進去
Matcher matcher = p.matcher(str);while(matcher.find()){//判斷下一個字符詞組是否爲三個字母組成的單詞
String group = matcher.group();//獲取匹配上的單詞
System.out.println(group.concat(" "));}}}
運行結果:
jia jin yao xia wan gao
Process finished with exit code 0
三、Math類概述和方法使用
A:Math類概述
Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函數。
B: 成員變量
publicstaticfinaldouble E : 自然底數
publicstaticfinaldouble PI: 圓周率
C:成員方法
publicstaticintabs(int a) 取絕對值
publicstaticdoubleceil(double a) 向上取整
publicstaticdoublefloor(double a) 向下取整
publicstaticintmax(int a,int b) 獲取最大值
publicstaticintmin(int a,int b) 獲取最小值
publicstaticdoublepow(double a,double b) 獲取a的b次冪
publicstaticdoublerandom() 獲取隨機數返回帶正號的 double 值,該值大於等於 0.0 且小於 1.0。
publicstaticintround(float a) 四捨五入
publicstaticdoublesqrt(double a) 獲取正平方根
演示:
publicclassMathDemo{publicstaticvoidmain(String[] args){//Math 數學工具類//屬性double pi = Math.PI;double e = Math.E;//方法double num = Math.random();//生成double類型隨機數 0-----1 之間//獲取最值int max = Math.max(19,20);//20int min = Math.min(20,40);//20//向上取整,向下取整double ceil = Math.ceil(1.9);//2.0double floor = Math.floor(1.9);//1.0//四捨五入long round = Math.round(1.48);//小數點後一位四捨五入 結果:1//求一個數的幾次冪double pow = Math.pow(2,3);//2的3次方//求平方根double sqrt = Math.sqrt(4);//2.0//求一個數的立方根double pow2 = Math.pow(8,1.0/3);//2.0//求一個數的絕對值int abs = Math.abs(-1);//1}}
四、Random類
A:Random類的概述
此類的實例用於生成僞隨機數流,使用48位的種子,如果用相同的種子創建兩個 Random 實例,
則對每個實例進行相同的方法調用序列,它們將生成並返回相同的數字序列。
B:構造方法
public Random() 沒有給定種子,使用的是默認的(當前系統的毫秒值)
public Random(long seed) 給定一個long類型的種子,給定以後每一次生成的隨機數是相同的
C:成員方法
public int nextInt()//沒有參數 表示的隨機數範圍 是int類型的範圍
public int nextInt(int n)//可以指定一個隨機數範圍
void nextBytes(byte[] bytes) 生成隨機字節並將其置於用戶提供的空的 byte 數組中。
示例:
publicclassRandomDemo{publicstaticvoidmain(String[] args){//生成隨機數據的類// 此類的實例用於生成僞隨機數流。此類使用 48 位的種子,
Random random =newRandom();//生成一些隨機整數
random.nextInt();//如果沒有傳入一個範圍,生成的隨機數就是int的範圍
random.nextInt(100);//範圍是0-99,包含0和99
random.nextDouble();//生成一個隨機double範圍內的數
random.nextBoolean();//生成一個隨機boolean值byte[] bytes=newbyte[10];//隨機一些byte範圍的數,填到這個數組中
random.nextBytes(bytes);show();show();show();}publicstaticvoidshow(){int[] arr=newint[10];//Random( long seed)//使用單個 long 種子創建一個新的隨機數生成器。//給定了種子,每次生成的隨機數都是一樣的
Random random =newRandom(1);for(int i =0; i <10; i++){int i1 = random.nextInt(10);
arr[i]=i1;}
System.out.println(Arrays.toString(arr));}}
運行結果:
[5,8,7,3,4,4,4,6,8,8][5,8,7,3,4,4,4,6,8,8][5,8,7,3,4,4,4,6,8,8]
Process finished with exit code 0