14.01_常见对象(正则表达式的概述和简单使用)
-
A:正则表达式
- 是指一个用来描述或者匹配一系列符合某个语法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。
- 作用:比如注册邮箱,邮箱有用户名和密码,一般会对其限制长度,这个限制长度的事情就是正则表达式做的
-
B:案例演示
-
需求:校验qq号码.
- 1:要求必须是5-15位数字
- 2:0不能开头
- 3:必须都是数字
-
a:非正则表达式实现
-
b:正则表达式实现
public class Demo1_Regex {public static void main(String[] args) {System.out.println(checkQQ("012345"));System.out.println(checkQQ("a1b345"));System.out.println(checkQQ("123456"));System.out.println(checkQQ("1234567890987654321"));String regex = "[1-9]\\d{4, 14}";System.out.println("2553868".matches(regex));System.out.println("012345".matches(regex));System.out.println("2553868abc".matches(regex));}public static boolean checkQQ(String qq) {boolean flag = true; //如果校验qq不符合要求就把flag置为false,如果符合要求直接返回if(qq.length() >= 5 && qq.length() <= 15) {if(!qq.startsWith("0")) {char[] arr = qq.toCharArray(); //将字符串转换成字符数组for (int i = 0; i < arr.length; i++) {char ch = arr[i]; //记录每一个字符if(!(ch >= '0' && ch <= '9')) {flag = false; //不是数字break;}}}else {flag = false; //以0开头,不符合qq标准}}else {flag = false; //长度不符合}return flag;}
}
-
14.02_常见对象(字符类演示)
-
A:字符类:
String regex = "[a-z&&[^m-p]]";
- [abc] a、b 或 c(简单类)
//[]代表单个字符
- [^abc] 任何字符,除了 a、b 或 c(否定)
System.out.println("10".matches(regex)); //false,10代表1字符和0字符,不是单个字符
- [a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
- [0-9] 0到9的字符都包括
- [a-d[m-p]] a 到 d 或 m 到 p:[a-dm-p](并集)
- [a-z&&[def]] d、e 或 f(交集)
- [a-z&&[^bc]] a 到 z,除了 b 和 c:[ad-z](减去)
- [a-z&&[^m-p]] a 到 z,而非 m 到 p:[a-lq-z](减去)
14.03_常见对象(预定义字符类演示)
-
A:预定义字符类
- . 任何字符。
- \d 数字:[0-9]
String regex = "\\d";// \代表转义字符,如果想表示\d的话,需要\\d
- \D 非数字: [^0-9]
- \s 空白字符:[ \t\n\x0B\f\r]
System.out.println(" ".matches(regex)); //true,一个tab键
System.out.println(" ".matches(regex)); //false,四个空格,并非一个空格,虽然与tab长度一致
- \S 非空白字符:[^\s]
- \w 单词字符:[a-zA-Z_0-9]
System.out.println("_".matches(regex));//true,0-9可以,11不可以
- \W 非单词字符:[^\w]
14.04_常见对象(数量词)
-
A:Greedy 数量词
- X?
X,一次或零次
String regex = "[abc]?";
System.out.println("".matches(regex));//true,这样子就是一次也没有
- X*
X,零次或多次
- X+
X,一次或多次
- X{n}
X,恰好 n 次
- X{n,}
X,至少 n 次
- X{n,m}
X,至少 n 次,但是不超过 m 次
String regex = "[abc]{5,15}";
System.out.println("abcbaabcabbabab".matches(regex));//true
- X?
14.05_常见对象(正则表达式的分割功能)
-
A:正则表达式的分割功能
- String类的功能:public String[] split(String regex)
-
B:案例演示
- 正则表达式的分割功能
-
String s = "金三胖.郭美美.李dayone";String[] arr = s.split("\\.");//通过正则表达式切割字符串,若只是.的话,会完全切割,需要转义,\.也不行,这也属于正则表达式的一种for (int i = 0; i < arr.length; i++) {System.out.println(arr[i]);}
14.06_常见对象(把给定字符串中的数字排序)
-
A:案例演示
-
需求:我有如下一个字符串:”91 27 46 38 50”,请写代码实现最终输出结果是:”27 38 46 50 91”
public static void main(String[] args) {String s = "91 27 46 38 50";//1,将字符串切割成字符串数组String[] sArr = s.split(" ");//2,将字符串转换成数字并将其存储在一个等长度的int数组中int[] arr = new int[sArr.length];for (int i = 0; i < arr.length; i++) {arr[i] = Integer.parseInt(sArr[i]); //将数字字符串转换成数字}//3,排序Arrays.sort(arr);//4,将排序后的结果遍历并拼接成一个字符串27 38 46 50 91/*String str = "";for (int i = 0; i < arr.length; i++) {if(i == arr.length - 1) {str = str + arr[i]; //27 38 46 50 91}else {str = str + arr[i] + " "; //27 38 46 50 }}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);}
-
14.07_常见对象(正则表达式的替换功能)
-
A:正则表达式的替换功能
- String类的功能:public String replaceAll(String regex,String replacement)
-
B:案例演示
-
正则表达式的替换功能
public static void main(String[] args) {String s = "wo111ai222heima";String regex = "\\d"; // \\d代表的是任意数字String s2 = s.replaceAll(regex, "");//用空串替换System.out.println(s2);}
-
14.08_常见对象(正则表达式的分组功能)
-
A:正则表达式的分组功能
- 捕获组可以通过从左到右计算其开括号来编号。例如,在表达式 ((A)(B?)) 中,存在四个这样的组:
-
1 ((A)(B(C))) 2 (A 3 (B(C)) 4 (C) 组零始终代表整个表达式。
-
B:案例演示
- a:切割
- 需求:请按照叠词切割: “sdqqfgkkkhjppppkl”;
- b:替换
-
需求:我我…我…我.要…要要…要学…学学…学.编…编编.编.程.程.程…程
-
将字符串还原成:“我要学编程”。
public static void main(String[] args) {//demo1();//demo2();String s = "我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程";String s2 = s.replaceAll("\\.+", ""); //去掉中间的点.String s3 = s2.replaceAll("(.)\\1+", "$1"); //前面这一组存的是“我”这个字,全都获取到之后,用$1替换,$1代表第一组中的内容(即(.)内的内容:“我”)System.out.println(s3);}public static void demo2() {//需求:请按照叠词切割: "sdqqfgkkkhjppppkl";String s = "sdqqfgkkkhjppppkl";String regex = "(.)\\1+";//+代表第一组出现一次到多次String[] arr = s.split(regex);for (int i = 0; i < arr.length; i++) {System.out.println(arr[i]);}}public static void demo1() {//叠词 快快乐乐,高高兴兴String regex = "(.)\\1(.)\\2";// \\1代表第一组又出现一次 \\2代表第二组又出现一次System.out.println("快快乐乐".matches(regex)); //trueSystem.out.println("快乐乐乐".matches(regex)); //falseSystem.out.println("高高兴兴".matches(regex)); /trueSystem.out.println("哇啦哇啦".matches(regex)); //false//叠词 wa啦wa啦,高兴高兴String regex2 = "(..)\\1";System.out.println("wa啦wa啦".matches(regex2)); //falseSystem.out.println("高兴高兴".matches(regex2)); //trueSystem.out.println("快快乐乐".matches(regex2)); //false}
-
- a:切割
14.09_常见对象(Pattern和Matcher的概述)
A:Pattern和Matcher的概述
B:模式和匹配器的典型调用顺序
* 通过JDK提供的API,查看Pattern类的说明* 典型的调用顺序是
* `Pattern p = Pattern.compile("a*b");//获取到正则表达式,a出现0次到多次,后面跟一个b`
* `Matcher m = p.matcher("aaaaab");//获取匹配器,看能否拿(上面的)正则匹配上`
* `boolean b = m.matches();//看是否能匹配,匹配就返回true`
14.10_常见对象(正则表达式的获取功能)
-
A:正则表达式的获取功能
- Pattern和Matcher的结合使用
-
B:案例演示
-
需求:把一个字符串中的手机号码获取出来
public static void main(String[] args) {//demo1();String s = "我的手机是18511866260,我曾用过18987654321,还用过18812345678";String regex = "1[3578]\\d{9}";//1确定开头,第二个在3578四个数字之中,从第三位开始是任意数字,且出现9次,这就是手机号码的正则表达式Pattern p = Pattern.compile(regex);Matcher m = p.matcher(s);/*boolean b1 = m.find();System.out.println(b1);System.out.println(m.group());boolean b2 = m.find();System.out.println(b2);System.out.println(m.group());*///循环打印3次while(m.find()) //Matcher类下的find():拿正则到字符串中寻找符合规则的子字符串System.out.println(m.group()); //Matcher类下的group():获取匹配的子字符串,必须先找再获取}public static void demo1() {Pattern p = Pattern.compile("a*b"); //获取到正则表达式Matcher m = p.matcher("aaaaab"); //获取匹配器boolean b = m.matches(); //看是否能匹配,匹配就返回true//若上面匹配手机号直接用这三行代码,会报错,因为它从“我”开始匹配System.out.println(b);System.out.println("aaaaab".matches("a*b")); //与上面四行结果一样}
-
14.11_常见对象(Math类概述和方法使用)
-
A:Math类概述**(所有成员都是静态)**
- Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
-
B:成员方法
- public static int abs(int a)
System.out.println(Math.abs(-10)); //取绝对值
- public static double ceil(double a)
System.out.println(Math.ceil(12.3)); //13,向上取整,但是结果是一个double
- public static double floor(double a)
System.out.println(Math.floor(12.3)); //12,向下取整,但是结果是一个double
- public static int max(int a,int b)
System.out.println(Math.max(20, 30)); //获取两个值中的最大值,min类似
- public static double pow(double a,double b)
System.out.println(Math.pow(2, 3)); //2.0 ^ 3.0,8,前面的数是底数,后面的数是指数
- public static double random()
System.out.println(Math.random()); //生成0.0到1.0之间的小数,包括0.0,不包括1.0
- public static int round(float a)
System.out.println(Math.round(12.3f)); //四舍五入,double类似
- public static double sqrt(double a)
System.out.println(Math.sqrt(4)); //2,开平方
- public static int abs(int a)
14.12_常见对象(Random类的概述和方法使用)
-
A:Random类的概述
- 此类用于产生随机数如果用相同的种子创建两个 Random 实例,
- 则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
-
B:构造方法
- public Random()
- public Random(long seed);//根据指定的seed计算,seed相同值也相同
-
C:成员方法
- public int nextInt()
- public int nextInt(int n);//要求掌握,生成在0到n范围内的随机数,包含0不包含n
14.13_常见对象(System类的概述和方法使用)
-
A:System类的概述
- System 类包含一些有用的类字段和方法。它不能被实例化。
-
B:成员方法
- public static void gc()
- public static void exit(int status)
- public static long currentTimeMillis()
- pubiic static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
-
C:案例演示
-
System类的成员方法使用
public static void main(String[] args) {//demo1();//demo2();//demo3();int[] src = {11,22,33,44,55};int[] dest = new int[8];for (int i = 0; i < dest.length; i++) {System.out.println(dest[i]);}System.out.println("--------------------------");System.arraycopy(src, 0, dest, 0, src.length); //将数组内容拷贝for (int i = 0; i < dest.length; i++) {System.out.println(dest[i]);}}public static void demo3() {long start = System.currentTimeMillis(); //1秒等于1000毫秒for(int i = 0; i < 1000; i++) {System.out.println("*");}long end = System.currentTimeMillis(); //获取当前时间的毫秒值System.out.println(end - start);//得到程序运行时间}public static void demo2() {System.exit(1); //非0状态是异常终止,退出jvmSystem.out.println("11111111111"); //此处无法输出,因为已退出}public static void demo1() {for(int i = 0; i < 100; i++) {new Demo();System.gc(); //运行垃圾回收器finalize(),相当于呼喊保洁阿姨}}}class Demo { //在一个源文件中不允许定义两个用public修饰的类@Override //重写finalize的方法public void finalize() {System.out.println("垃圾被清扫了"); //非手动调用,垃圾到量后会自动执行}
-
14. 14_常见对象(BigInteger类的概述和方法使用)
-
A:BigInteger的概述
- 可以让超过Integer范围内的数据进行运算
-
B:构造方法
- public BigInteger(String val)
-
C:成员方法
-
public BigInteger add(BigInteger val)
-
public BigInteger subtract(BigInteger val)
-
public BigInteger multiply(BigInteger val)
-
public BigInteger divide(BigInteger val)
-
public BigInteger[] divideAndRemainder(BigInteger val)
public static void main(String[] args) {//long num = 123456789098765432123L;//String s = "123456789098765432123";BigInteger bi1 = new BigInteger("100");BigInteger bi2 = new BigInteger("2");System.out.println(bi1.add(bi2)); //+System.out.println(bi1.subtract(bi2)); //-System.out.println(bi1.multiply(bi2)); //*System.out.println(bi1.divide(bi2)); ///(除)BigInteger[] arr = bi1.divideAndRemainder(bi2); //取除数和余数,返回两个数的数组for (int i = 0; i < arr.length; i++) {System.out.println(arr[i]);}}
-
14.15_常见对象(BigDecimal类的概述和方法使用)
-
A:BigDecimal的概述
- 由于在运算的时候,float类型和double很容易丢失精度,演示案例。
- 所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal
- 不可变的、任意精度的有符号十进制数。
-
B:构造方法
- public BigDecimal(String val)
-
C:成员方法
- public BigDecimal add(BigDecimal augend)
- public BigDecimal subtract(BigDecimal subtrahend)
- public BigDecimal multiply(BigDecimal multiplicand)
- public BigDecimal divide(BigDecimal divisor)
-
D:案例演示
-
BigDecimal类的构造方法和成员方法使用
public static void main(String[] args) {//System.out.println(2.0 - 1.1);//结果:0.8999999999,精度损失/*BigDecimal bd1 = new BigDecimal(2.0); //选择传入double参数,这种方式在开发中不推荐,因为不够精确BigDecimal bd2 = new BigDecimal(1.1); System.out.println(bd1.subtract(bd2));*//*BigDecimal bd1 = new BigDecimal("2.0"); //通过构造中传入字符串的方式,开发时推荐BigDecimal bd2 = new BigDecimal("1.1");System.out.println(bd1.subtract(bd2)); //0.9,结果精确*/BigDecimal bd1 = BigDecimal.valueOf(2.0); //这种用方法的方式在开发中也是推荐的BigDecimal bd2 = BigDecimal.valueOf(1.1); System.out.println(bd1.subtract(bd2)); //0.9}
-
14.16_常见对象(Date类的概述和方法使用)(掌握)
-
A:Date类的概述**(Date是util包下的,不能导入sql包)**
- 类 Date 表示特定的瞬间,精确到毫秒。
-
B:构造方法**(其他方法都过时了)**
- public Date()
- public Date(long date)
-
C:成员方法
-
public long getTime()
-
public void setTime(long time)
public static void main(String[] args) {//demo1();//demo2();Date d1 = new Date(); d1.setTime(1000); //1970年的08:00:01,设置毫秒值,改变 时间对象,set方法System.out.println(d1); //Thu Jan 01 08:00:01 CST 1970}public static void demo2() {Date d1 = new Date(); System.out.println(d1.getTime()); //通过时间对象 获取 毫秒值,get方法System.out.println(System.currentTimeMillis()); //通过系统类的方法获取当前时间毫秒值}public static void demo1() {Date d1 = new Date(); //如果没有传参数代表的是当前时间System.out.println(d1);Date d2 = new Date(0); //如果构造方法中参数传为0代表的是1970年1月1日System.out.println(d2); //通过毫秒值创建时间对象}
-
14.17_常见对象(SimpleDateFormat类实现日期和字符串的相互转换)(掌握)
-
A:DateFormat类的概述
- DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。是抽象类,所以使用其子类SimpleDateFormat
-
B:SimpleDateFormat构造方法
- public SimpleDateFormat()
- public SimpleDateFormat(String pattern)
-
C:成员方法
-
public final String format(Date date) //将日期对象转换为字符串
-
public Date parse(String source) //将时间字符串转换成日期对象
public static void main(String[] args) throws ParseException {//demo1();//demo2();//demo3();//将时间字符串转换成日期对象String str = "2000年08月08日 08:08:08";SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");Date d = sdf.parse(str); //此处被抛出异常 //将时间字符串转换成日期对象,System.out.println(d); //Tue Aug 08 08:08:08 CST 2000System.out.println(sdf.format(d)); //2000年08月08日 08:08:08}public static void demo3() {Date d = new Date(); //获取当前时间对象SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //创建日期格式化类对象System.out.println(sdf.format(d)); //2019/4/3 20:39:44,将日期对象转换为字符串,若只有两个y,即19,其他少一个还是原来的值}public static void demo2() {Date d = new Date(); //获取当前时间对象SimpleDateFormat sdf = new SimpleDateFormat(); //创建日期格式化类对象System.out.println(sdf.format(d)); //19-5-2 下午3:11,format()返回一个string字符串}public static void demo1() {//DateFormat df = new DateFormat(); //DateFormat是抽象类,不允许实例化//DateFormat df1 = new SimpleDateFormat();DateFormat df1 = DateFormat.getDateInstance(); //相当于 父类引用指向子类对象,右边的方法返回一个 子类对象}
-
14.18_常见对象(你来到这个世界多少天案例)(掌握)
-
A:案例演示
-
需求:算一下你来到这个世界多少天?
public static void main(String[] args) throws ParseException {//1,将生日字符串和今天字符串存在String类型的变量中String birthday = "1983年07月08日";String today = "2088年6月6日";//2,定义日期格式化对象SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");//3,将日期字符串转换成日期对象Date d1 = sdf.parse(birthday);Date d2 = sdf.parse(today);//4,通过日期对象后期时间毫秒值long time = d2.getTime() - d1.getTime();//5,将两个时间毫秒值相减除以1000,再除以60,再除以60,再除以24得到天System.out.println(time / 1000 / 60 / 60 / 24 );}
-
14.19_常见对象(Calendar类的概述和获取日期的方法)(掌握)
-
A:Calendar类的概述**(替代了Date类中的很多方法)**
- Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
-
B:成员方法
- public static Calendar getInstance() //使用默认时区和语言环境获得一个日历。
- public int get(int field)
14.20_常见对象(Calendar类的add()和set()方法)(掌握)
-
A:成员方法
- public void add(int field,int amount)
- public final void set(int year,int month,int date)
-
B:案例演示
-
Calendar类的成员方法使用
public static void main(String[] args) {//demo1();Calendar c = Calendar.getInstance(); //父类引用指向子类对象//c.add(Calendar.MONTH, -1); //对指定的字段进行向前减或向后加,9月->8月//c.set(Calendar.YEAR, 2000); //修改指定字段,年份被修改为2000c.set(2000, 7, 8); //2000年8月8日,月份+1System.out.println(c.get(Calendar.YEAR) + "年" + getNum((c.get(Calendar.MONTH)+1)) + "月" + getNum(c.get(Calendar.DAY_OF_MONTH)) + "日" + getWeek(c.get(Calendar.DAY_OF_WEEK)));}public static void demo1() {Calendar c = Calendar.getInstance(); //父类引用指向子类对象//System.out.println(c);System.out.println(c.get(Calendar.YEAR)); //通过字段获取年System.out.println(c.get(Calendar.MONTH)); //通过字段后期月,但是月是从0开始编号的,显示4,其实表示5System.out.println(c.get(Calendar.DAY_OF_MONTH)); //月中的第几天System.out.println(c.get(Calendar.DAY_OF_WEEK));//周日是第一天,周六是最后一天System.out.println(c.get(Calendar.YEAR) + "年" + getNum((c.get(Calendar.MONTH)+1)) + "月" + getNum(c.get(Calendar.DAY_OF_MONTH)) + "日" + getWeek(c.get(Calendar.DAY_OF_WEEK)));}/** 将星期存储表中进行查表* 1,返回值类型String* 2,参数列表int week*/public static String getWeek(int week) {String[] arr = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};//因为week给的是1-7,索引0处无值对应return arr[week];}/** 如果是个数数字前面补0* 1,返回值类型String类型* 2,参数列表,int num*/public static String getNum(int num) {/*if(num > 9) {return "" + num;}else {return "0" + num;}*/return num > 9 ? "" + num : "0" + num;}
-
14.21_常见对象(如何获取任意年份是平年还是闰年)(掌握)
- A:案例演示
-
需求:键盘录入任意一个年份,判断该年是闰年还是平年
public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入年份,判断该年份是闰年还是平年:");//int year = sc.nextInt(); //将输入信息的下一个标记扫描为一个 intString line = sc.nextLine(); //录入数字字符串int year = Integer.parseInt(line); //将数字字符串转换成数字boolean b = getYear(year);System.out.println(b);}private static boolean getYear(int year) {//2,创建Calendar c = Calendar.getInstance();Calendar c = Calendar.getInstance();//设置为那一年的3月1日c.set(year, 2, 1);//将日向前减去1c.add(Calendar.DAY_OF_MONTH, -1);//判断是否是29天,闰年2月有29天return c.get(Calendar.DAY_OF_MONTH) == 29;}
-