枚举
不使用枚举存在的问题
public class TestPerson {
public static void main(String[] args) {
//创建对象
person p1 = new person("小明", "妖");
System.out.println(p1);
//此时由于性别是String类型,所以任意字符串都可以,但是性别也应该有要求,男、女
}
}
枚举的作用与应用场景
枚举是一种特殊的类,就是把所有可能的情况一一列举出来。
应用场景:某一个数据的值或个数是有限的,就可以使用枚举把其值一一列举出来。
枚举的基本语法
定义枚举的格式
public enum 枚举名{
//枚举项
枚举项1,枚举项2;
}
enum Sex { BOY, GIRL; // 男,女 }
案例
//性别的枚举
public enum SexEnum {
Male, Female;//男、女
}
public class person {
private String name;
//以前的性别是String类型,导致任何值都可以
//private String sex;
//有枚举后
private SexEnum sex;
public person() {
}
public person(String name, SexEnum sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SexEnum getSex() {
return sex;
}
public void setSex(SexEnum sex) {
this.sex = sex;
}
@Override
public String toString() {
return "person{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}';
}
}
public class TestPerson {
public static void main(String[] args) {
//创建对象
person p1 = new person("小明", SexEnum.Male);
System.out.println(p1);
//此时由于性别是String类型,所以任意字符串都可以,但是性别也应该有要求,男、女
}
}
枚举的本质
枚举的本质其实就是一个类,枚举项就是一个对象
enum Sex { BOY, GIRL; // 男,女 }
// 枚举的本质是一个类,我们刚才定义的Sex枚举相当于下面的类
final class SEX extends java.lang.Enum<SEX> {
public static final SEX BOY = new SEX();
public static final SEX GIRL = new SEX();
public static SEX[] values();
public static SEX valueOf(java.lang.String);
static {};
}
//构造方法是私有化的,外界无法new对象
JDK8的其他新特性
方法引用
概述
就是把已经存在的方法,直接拿过来使用。
public class MethodDemo {
public static void main(String[] args) {
//使用实现接口的方法
new Thread(new MyRunnable()).start();
//方法引用,因为方法中参数和返回值和函数式中的一致,就可以引用过来
new Thread(Dog::eat).start();
//引用成员方法【用过对象名】
Dog dd = new Dog();
new Thread(dd::eat1).start();
}
}
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("执行了");
}
}
public class Dog {
public static void eat() {
System.out.println("吃骨头");
}
public void eat1() {
System.out.println("吃肉");
}
}
基本格式
- 通过类名引用其中的静态方法 类名::静态方法名
- 通过对象引用其中的非静态方法 对象名::普通方法名
- 通过类名引用其构造方法 类名::new
- 通过数组引用其构造方法 数据类型[ ]::new
System.out.println方法引用的代码案例
public class SystemDemo {
public static void main(String args[]) {
List names = new ArrayList();
names.add("大明");
names.add("二明");
names.add("小明");
//匿名内部类
names.forEach(new Consumer<String>() {
@Override
public void accept(String o) {
System.out.println(o);
}
});
//通过方法引用
names.forEach(System.out::println);
}
}
正则表达式
正则表达式的概念
下面的程序让用户输入一个QQ号码,我们要验证:
QQ号码必须是5–15位长度
而且必须全部是数字
而且首位不能为0
//使用正则表达式
boolean b = qq.matches("[1-9]\\d{4,14}");//matches匹配
System.out.println(b);
正则也是一个字符串,但是里面的内容表示某种规则。
正则表达式-字符类
- [abc]:代表a或者b,或者c字符中的一个。
- [^abc] :代表除a,b,c以外的任何字符。
- [a-z]:代表a-z的所有小写字符中的一个。
- [A-Z]:代表A-Z的所有大写字符中的一个。
- [0-9]:代表0-9之间的某一个数字字符。
- [a-zA-Z0-9]:代表a-z或者A-Z或者0-9之间的任意一个字符。
- [a-dm-p]:a 到 d 或 m 到 p之间的任意一个字符。
代码案例
public class Demo {
public static void main(String[] args) {
String str = "ead";
//1.验证str是否以h开头,以d结尾,中间是a,e,i,o,u中某个字符
String regex = "h[aeiou]d";
System.out.println("1." + str.matches(regex));
//2.验证str是否以h开头,以d结尾,中间不是a,e,i,o,u中的某个字符
regex = "h[^aeiou]d";
System.out.println("2." + str.matches(regex));
//3.验证str是否a-z的任何一个小写字符开头,后跟ad
regex = "[a-z]ad";
System.out.println("3." + str.matches(regex));
//4.验证str是否以a-d或者m-p之间某个字符开头,后跟ad
regex = "[[a-d][m-p]]ad";
System.out.println("4." + str.matches(regex)); } }
正则表达式-逻辑运算符
- &&:并且
- | :或者
代码案例
public class Demo {
public static void main(String[] args) {
String str = "had";
//1.要求字符串是否是除a、e、i、o、u外的其它小写字符开头,后跟ad
String regex = "[a-z&&[^aeiou]]ad";
System.out.println("1." + str.matches(regex));
//2.要求字符串是aeiou中的某个字符开头,后跟ad
regex = "[a|e|i|o|u]ad";//这种写法相当于:regex = "[aeiou]ad";
System.out.println("2." + str.matches(regex));
}
}
正则表达式-预定义字符
- “.” : 匹配任何字符。
- “\d”:任何数字[0-9]的简写;
- “\D”:任何非数字[^0-9]的简写;
- “\s”: 空白字符:[ \t\n\x0B\f\r] 的简写
- “\S”: 非空白字符:[^\s] 的简写
- “\w”:单词字符:[a-zA-Z_0-9]的简写
- “\W”:非单词字符:[^\w]
代码案例
public class Demo {
public static void main(String[] args) {
String str = "258";
//1.验证str是否3位数字
String regex = "\\d\\d\\d";
System.out.println("1." + str.matches(regex));
//2.验证手机号:1开头,第二位:3/5/8,剩下9位都是0-9的数字
str = "13513153355";
//要验证的字符串
regex = "1[3|5|8]\\d\\d\\d\\d\\d\\d\\d\\d\\d";//正则表达式
System.out.println("2." + str.matches(regex));
//3.验证字符串是否以h开头,以d结尾,中间是任何字符
str = "had";//要验证的字符串
regex = "h.d";//正则表达式
System.out.println("3." + str.matches(regex));
//4.验证str是否是:had.
str = "had.";//要验证的字符串
regex = "had\\.";//\\.代表'.'符号,因为.在正则中被预定义为"任意字符",不能直接使用
System.out.println("4." + str.matches(regex));
}
}
正则表达式-数量池
- X? : 0次或1次
- X* : 0次到多次
- X+ : 1次或多次
- X{n} : 恰好n次
- X{n,} : 至少n次
- X{n,m}: n到m次(n和m都是包含的)
代码案例
public class Demo {
public static void main(String[] args) {
String str = "";
//1.验证str是否是三位数字
str = "012";
String regex = "\\d{3}";
System.out.println("1." + str.matches(regex));
//2.验证str是否是多位数字
str = "88932054782342";
regex = "\\d+";
System.out.println("2." + str.matches(regex));
//3.验证str是否是手机号:
str = "13813183388";
regex = "1[358]\\d{9}";
System.out.println("3." + str.matches(regex));
//4.验证小数:必须出现小数点,但是只能出现1次
String s2 = "3.1";
regex = "\\d*\\.{1}\\d+";
System.out.println("4." + s2.matches(regex));
//5.验证小数:小数点可以不出现,也可以出现1次
regex = "\\d+\\.?\\d+";
System.out.println("5." + s2.matches(regex));
//6.验证小数:要求匹配:3、3.、3.14、+3.14、-3. s2 = "-3.";
regex = "[+-]\\d+\\.?\\d*";
System.out.println("6." + s2.matches(regex));
//7.验证qq号码:1).5--15位;2).全部是数字;3).第一位不是0
s2 = "1695827736";
regex = "[1-9]\\d{4,14}";
System.out.println("7." + s2.matches(regex)); } }
正则表达式-分组括号
public class Demo {
public static void main(String[] args) {
String str = "DG8FV-B9TKY-FRT9J-99899-XPQ4G"; //验证这个序列号:分为5组,每组之间使用-隔开,每组由5位A-Z或者0-9的字符组成 S
tring regex = "([A-Z0-9]{5}-){4}[A-Z0-9]{5}";
System.out.println(str.matches(regex));
}
}
String的split方法
public class Demo {
public static void main(String[] args) {
String str = "18 4 567 99 56";
String[] strArray = str.split(" +");
for (int i = 0; i < strArray.length; i++) {
System.out.println(strArray[i]);
}
}
}
String类的replaceAll方法中使用正则表达式
public class Demo {
public static void main(String[] args) {
//将下面字符串中的"数字"替换为"*"
String str = "jfdk432jfdk2jk24354j47jk5l31324";
System.out.println(str.replaceAll("\\d+", "*"));
}
}
常用正则表达式练习
- 验证给定的字符串全部由中文组成
public class demo01 {
public static void main(String[] args) {
String str = "我爱中国";
String regex = "^[\\u4e00-\\u9fa5]{0,}$";
System.out.println(str.matches(regex));
}
}
- 校验键盘录入的身份证号是否合法
public class demo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入18位身份证号:");
String str = scanner.next();
String regex = "/(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)/";
if (str.matches(regex)) {
System.out.println("身份证号码合法");
System.exit(0);
}else {
System.out.println("身份证号码不合法请重新输入");
}
}
}
}
- 校验键盘录入的电子邮箱是否合法
public class demo01 {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入电子邮箱:");
String str = scanner.next();
String regex="^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
System.out.println(str.matches(regex));
}
}
- 完成叠词的替换
public class demo01 {
public static void main(String[] args) {
String str = "我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程";
String regex = "\\.{1,}";
//先去除所有的点
str = str.replaceAll(regex, "");
//去除叠词
str = str.replaceAll("(.)\\1+", "$1");
System.out.println(str);
}
}
- 把“E:\Workspace\201909\RegexTest.java”文件路径,按照 \ 切割
public class demo01 {
public static void main(String[] args) {
String path = "E:\\Workspace\\201909\\RegexTest.java";
String[] split = path.split("\\\\");
for (int i = 0; i < split.length; i++) {
System.out.println(split[i]);
}
}
}