您現在的位置是:首頁 > 農業

「Java基礎12」正則表示式、Lambda表示式和排序演算法

由 IT少安 發表于 農業2021-12-27
簡介1 元字元字串中提供了匹配正則表示式方法: 匹配成功返回true,否則返回falsepublic boolean matches(String regex) 使用正則替換所有public String replaceAll(Str

程式是什麼

目錄

1。正則表示式

1。1 元字元

1。2 示例

2。Lambda表示式

2。1 示例

2。2 Lambda表示式簡寫

3。 排序演算法

3。1 氣泡排序

3。2 選擇排序

3。3 二分選擇(非排序演算法)

1。正則表示式

正則表示式是用一些規定的字元制定規則,用這個規則來校驗資料的合法性。

比如在使用者輸入註冊暱稱時,必須對其進行合法性校驗。比如長度,大家可能會想, 直接用字串長度方法加if不就行了,但是如果判斷長度的同時還需要判斷字串 是否有敏感詞。你可能會說,多重判斷不是就可以實現功能,少量可以實現,數量

較多的話那可想而知程式碼量。

1。1 元字元

字串中提供了匹配正則表示式方法:

// 匹配成功返回true,否則返回falsepublic boolean matches(String regex)// 使用正則替換所有public String replaceAll(String regex,String newStr)// 使用正則分割字串public String[]split(String regex):

匹配單個字元:

字元

說明

[a,b,c]

符合abc中任意一個即可

[^a,b,c]

除abc中任意一個即可

[a-zA-Z]

包含a-z或A-Z中任意一個即可

[a-p[q-z]]

a到p或者q-z

[a-z&&[d,e,f]]

包含a-z並且符合d,e,f

[a-z&&[^d,e,f]]

包含a-z並且除d,e,f

匹配任意字元

\d

匹配一個數值

\D

匹配字元為非數值

\s

匹配一個空白字元: [ \t\n\x0B\f\r]

\S

匹配非空白字串

\w

匹配數字,字元和下劃線

\W

匹配非單詞字元

匹配多個字元:

字元

說明

x?

x匹配一次或者0次

x*

x匹配0次或者多次

x+

x匹配一次或者多次

x{n}

x匹配n次

x{n,}

x匹配至少n次

x{n,m}

x匹配n到m次

1。2 示例

public class StringMatchesTest { public static void main(String[] args) { System。out。println(“a”。matches(“[a,b,c,d,e]”)); System。out。println(“abc”。matches(“[a-z]”)); System。out。println(“abc”。matches(“[a-z]+”)); System。out。println(“9”。matches(“[\\d]”)); System。out。println(“991607476”。matches(“[\\d]{6,20}”)); String temp = “asb121b4b54jb656”; // 將所有數值替換成A String a = temp。replaceAll(“[\\d]+”, “A”); System。out。println(a); // 以字母進行分割 System。out。println(Arrays。toString(temp。split(“[a-zA-Z]+”))); }}

2。Lambda表示式

這是一個java8新特性之一,簡化匿名類內部類寫法。 Lambda表示式實現的匿名內部類必須是函式式介面

// Lambda格式(被重寫方法的形參列表)->{ 程式碼; }

函式式介面:

@FunctionInterfacepublic interface 介面名 { // 僅且只能有這一個抽象方法 返回值型別 方法名稱(引數列表);}

2。1 示例

// 介面@FunctionalInterfacepublic interface Animal { void bark();}// 測試類public class LambdaTest { public static void main(String[] args) { // 以往使用匿名內部類 animalBark(new Animal() { @Override public void bark() { System。out。println(“現在是透過匿名內部類實現的,喵喵喵。。。”); } }); // 使用Lambda表示式 animalBark(() -> { System。out。println(“現在是透過Lambda實現的,汪汪汪。。。”); }); } public static void animalBark(Animal animal) { animal。bark(); }}

2。2 Lambda表示式簡寫

引數型別可以不寫

只有一個引數時,引數型別和()可以省略

如果實現類只有一行程式碼,則可以省略{},同時也要省略;

如果實現的程式碼只有一行並且這行程式碼為return語句,{}和;可以省略並且return也要省略

// 示例@FunctionalInterfacepublic interface FunctionInterface1 { int add(int x, int y);}@FunctionalInterfacepublic interface FunctionInterface2 { void showParam(String str);}public class LambdaTest2 { public static void main(String[] args) { // 就是如此簡單 showFunctionInterface1((a, b) -> a * 2 + b * 2); showFunctionInterface2(a -> a。trim()); } public static void showFunctionInterface1(FunctionInterface1 functionInterface1) { int add = functionInterface1。add(1, 2); System。out。println(add); } public static void showFunctionInterface2(FunctionInterface2 functionInterface2) { functionInterface2。showParam(“Hello World”); }}

3。 排序演算法

從字面意思理解,是對資料進行一定規則的排序。比如全校學生的某課成績進行正 序排序。需求並不難實現,但是要在計算速度上有要求就變得有意思了。這時演算法 就派上用場,最少的資源進行相同的運算。使用不同的演算法對下列陣列進行排序。

[94,58,95,4,13,18,70,0,59,21]

3。1 氣泡排序

public class Bubble { public static int[] x = {94, 58, 95, 4, 13, 18, 70, 0, 59, 21}; public static void main(String[] args) { System。out。println(“排序前:” + Arrays。toString(Bubble。x)); for (int i = 0; i < x。length - 1; i++) { for (int y = 0; y < x。length - 1 - i; y++) { if (x[y] < x[y + 1]) { int temp = x[y]; x[y] = x[y + 1]; x[y + 1] = temp; } } } System。out。println(“排序後:” + Arrays。toString(Bubble。x)); }}

下圖是網上找的gif圖,上面寫的與這個有點出入,我是向陣列尾部冒泡的:

「Java基礎12」正則表示式、Lambda表示式和排序演算法

3。2 選擇排序

public class Select { public static void main(String[] args) { System。out。println(“排序前:” + Arrays。toString(Bubble。x)); // 進行正序排序 for (int i = 0; i < Bubble。x。length - 1; i++) { for (int y = i + 1; y < Bubble。x。length; y++) { if (Bubble。x[i] < Bubble。x[y]) { int temp = Bubble。x[i]; Bubble。x[i] = Bubble。x[y]; Bubble。x[y] = temp; } } } System。out。println(“排序後:” + Arrays。toString(Bubble。x)); }}

思路如下圖:

「Java基礎12」正則表示式、Lambda表示式和排序演算法

3。3 二分選擇(非排序演算法)

二分法是為了實現快速找到查詢的演算法。使用此方法必須先對陣列進行排序。

示例:

對以上方法進行排序,然後以最快的速度查詢到13。每次取一般進行判斷,如果小於

則向下再取一半,否則向上取一半。

public class Dichotomy { public static void main(String[] args) { // 先為陣列進行排序 Bubble。sort(); int length = Bubble。x。length; int index = length / 2; int select = 95; while (index >= 0) { if (Bubble。x[index] == select) { break; } if (Bubble。x[index] > select) { index = index + (length - index) / 2; } if (Bubble。x[index] < select) { index = index / 2; } } System。out。println(index); }}

本章結束,用於個人學習和小白入門,大佬勿噴!希望大家多多點贊收藏支撐支撐!

原始碼 【GitHub】 【碼雲】

推薦文章