B:案例演示
需求:校验qq号码.
a:非正则表达式实现
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));
}
/*
* 需求:校验qq号码.
* 1:要求必须是5-15位数字
* 2:0不能开头
* 3:必须都是数字
* 校验qq
* 1,明确返回值类型boolean
* 2,明确参数列表String qq
*/
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;
}
}
public class Demo2_Regex {
/**
* [abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a 到 z 或 A 到 Z,两头的字母包括在内(范围)
[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](减去)
*/
public static void main(String[] args) {
//demo1();
//demo2();
//demo3();
//demo4();
//demo5();
//demo6();
String regex = "[a-z&&[^m-p]]";
System.out.println("m".matches(regex));
System.out.println("a".matches(regex));
System.out.println("z".matches(regex));
System.out.println("n".matches(regex));
}
private static void demo6() {
String regex = "[a-z&&[^bc]]";
System.out.println("a".matches(regex));
System.out.println("b".matches(regex));
System.out.println("1".matches(regex));
}
private static void demo5() {
String regex = "[a-z&&[def]]"; //取交集
System.out.println("a".matches(regex));
System.out.println("d".matches(regex));
}
private static void demo4() {
String regex = "[a-d[m-p]]";
System.out.println("a".matches(regex));
System.out.println("m".matches(regex));
System.out.println("n".matches(regex));
}
private static void demo3() {
String regex = "[a-zA-Z]";
System.out.println("a".matches(regex));
System.out.println("A".matches(regex));
System.out.println("z".matches(regex));
System.out.println("Z".matches(regex));
System.out.println("1".matches(regex));
System.out.println("%".matches(regex));
}
private static void demo2() {
String regex = "[^abc]";
System.out.println("a".matches(regex));
System.out.println("b".matches(regex));
System.out.println("c".matches(regex));
System.out.println("d".matches(regex));
System.out.println("1".matches(regex));
System.out.println("%".matches(regex));
System.out.println("10".matches(regex)); //10代表1字符和0字符,不是单个字符
}
private static void demo1() {
String regex = "[abc]"; //[]代表单个字符
System.out.println("a".matches(regex));
System.out.println("b".matches(regex));
System.out.println("c".matches(regex));
System.out.println("d".matches(regex));
System.out.println("1".matches(regex));
System.out.println("%".matches(regex));
}
}
public class Demo3_Regex {
public static void main(String[] args) {
//demo1();
//demo2();
//demo3();
//demo4();
//demo5();
//demo6();
String regex = "\\W";
System.out.println("a".matches(regex));
System.out.println("z".matches(regex));
System.out.println("_".matches(regex));
System.out.println("%".matches(regex));
}
private static void demo6() {
String regex = "\\w";
System.out.println("a".matches(regex));
System.out.println("z".matches(regex));
System.out.println("_".matches(regex));
System.out.println("%".matches(regex));
}
private static void demo5() {
String regex = "\\S";
System.out.println(" ".matches(regex));
System.out.println(" ".matches(regex));
System.out.println("a".matches(regex));
}
private static void demo4() {
String regex = "\\s";
System.out.println(" ".matches(regex));
System.out.println(" ".matches(regex)); //一个tab键
System.out.println(" ".matches(regex)); //四个空格
}
private static void demo3() {
String regex = "\\D";
System.out.println("0".matches(regex));
System.out.println("9".matches(regex));
System.out.println("a".matches(regex));
}
private static void demo2() {
String regex = "\\d"; //\代表转义字符,如果想表示\d的话,需要\\d
System.out.println("0".matches(regex));
System.out.println("a".matches(regex));
System.out.println("9".matches(regex));
}
private static void demo1() {
String regex = "..";
System.out.println("a".matches(regex));
System.out.println("ab".matches(regex));
}
}
public class Demo4_Regex {
public static void main(String[] args) {
//demo1();
//demo2();
//demo3();
//demo4();
//demo5();
String regex = "[abc]{5,15}";
System.out.println("abcba".matches(regex));
System.out.println("abcbaabcabbabab".matches(regex));
System.out.println("abcb".matches(regex));
System.out.println("abcbaabaabcbaaba".matches(regex));
}
public static void demo5() {
String regex = "[abc]{5,}";
System.out.println("abcba".matches(regex));
System.out.println("abcbaabcabbabab".matches(regex));
System.out.println("abcb".matches(regex));
System.out.println("abcbaaba".matches(regex));
}
public static void demo4() {
String regex = "[abc]{5}";
System.out.println("abcba".matches(regex));
System.out.println("abcbaabcabbabab".matches(regex));
System.out.println("abcb".matches(regex));
System.out.println("abcbaaba".matches(regex));
}
public static void demo3() {
String regex = "[abc]+";
System.out.println("".matches(regex));
System.out.println("a".matches(regex));
System.out.println("aaaaabbbbccccc".matches(regex));
}
public static void demo2() {
String regex = "[abc]*";
System.out.println("".matches(regex));
System.out.println("abc".matches(regex));
System.out.println("a".matches(regex));
}
public static void demo1() {
String regex = "[abc]?";
System.out.println("a".matches(regex));
System.out.println("b".matches(regex));
System.out.println("c".matches(regex));
System.out.println("d".matches(regex));
System.out.println("".matches(regex));
}
}
public class Demo5_Split {
/**
* @param args
*/
public static void main(String[] args) {
String s = "金三胖.郭美美.李dayone";
String[] arr = s.split("\\."); //通过正则表达式切割字符串
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("11111111111111111");
}
}
import java.util.Arrays;
public class Test1 {
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);
}
}
public class Demo6_ReplaceAll {
/**
* * A:正则表达式的替换功能
* String类的功能:public String replaceAll(String regex,String replacement)
*/
public static void main(String[] args) {
String s = "wo111ai222heima";
String regex = "\\d"; //\\d代表的是任意数字
String s2 = s.replaceAll(regex, "");
System.out.println(s2);
}
}
1 ((A)(B(C)))
2 (A
3 (B(C))
4 (C)
组零始终代表整个表达式。
public class Demo7_Regex {
public static void main(String[] args) {
//demo1();
//demo2();
/*
* 需求:我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程
将字符串还原成:“我要学编程”。
*/
String s = "我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程";
String s2 = s.replaceAll("\\.+", "");
String s3 = s2.replaceAll("(.)\\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));
System.out.println("快乐乐乐".matches(regex));
System.out.println("高高兴兴".matches(regex));
System.out.println("死啦死啦".matches(regex));*/
//叠词 死啦死啦,高兴高兴
String regex2 = "(..)\\1";
System.out.println("死啦死啦".matches(regex2));
System.out.println("高兴高兴".matches(regex2));
System.out.println("快快乐乐".matches(regex2));
}
}
B:模式和匹配器的典型调用顺序
通过JDK提供的API,查看Pattern类的说明
典型的调用顺序是
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo8_Pattern {
/**
* Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
* A:正则表达式的获取功能
* Pattern和Matcher的结合使用
* B:案例演示
* 需求:把一个字符串中的手机号码获取出来
*/
public static void main(String[] args) {
//demo1();
String s = "我的手机是18511866260,我曾用过18987654321,还用过18812345678";
String regex = "1[3578]\\d{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());*/
while(m.find())
System.out.println(m.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")); //与上面的结果一样
}
}
代码在上面演示
public class Demo1_Math {
public static void main(String[] args) {
System.out.println(Math.PI);
System.out.println(Math.abs(-10)); //取绝对值
//ceil天花板
/*
* 13.0
* 12.3
* 12.0
*/
System.out.println(Math.ceil(12.3)); //向上取整,但是结果是一个double
System.out.println(Math.ceil(12.99));
System.out.println("-----------");
//floor地板
/*
* 13.0
* 12.3
* 12.0
*/
System.out.println(Math.floor(12.3)); //向下取整,但是结果是一个double
System.out.println(Math.floor(12.99));
//获取两个值中的最大值
System.out.println(Math.max(20, 30));
//前面的数是底数,后面的数是指数
System.out.println(Math.pow(2, 3)); //2.0 ^ 3.0
//生成0.0到1.0之间的所以小数,包括0.0,不包括1.0
System.out.println(Math.random());
//四舍五入
System.out.println(Math.round(12.3f));
System.out.println(Math.round(12.9f));
//开平方
System.out.println(Math.sqrt(4));
System.out.println(Math.sqrt(2));
System.out.println(Math.sqrt(3));
}
}
import java.util.Random;
public class Demo2_Random {
/**
* * A:Random类的概述
* 此类用于产生随机数如果用相同的种子创建两个 Random 实例,
* 则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
* B:构造方法
* public Random()
* public Random(long seed)
* C:成员方法
* public int nextInt()
* public int nextInt(int n)(重点掌握)
*/
public static void main(String[] args) {
Random r = new Random();
/*int x = r.nextInt();
System.out.println(x);*/
for(int i = 0; i < 10; i++) {
//System.out.println(r.nextInt());
System.out.println(r.nextInt(100)); //要求掌握,生成在0到n范围内的随机数,包含0不包含n
}
/*
* -1244746321
1060493871
-1244746321
1060493871
*/
/*Random r2 = new Random(1001);
int a = r2.nextInt();
int b = r2.nextInt();
System.out.println(a);
System.out.println(b);*/
}
}
public class Demo3_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状态是异常终止,退出jvm
System.out.println("11111111111");
}
public static void demo1() {
for(int i = 0; i < 100; i++) {
new Demo();
System.gc(); //运行垃圾回收器,相当于呼喊保洁阿姨
}
}
}
class Demo { //在一个源文件中不允许定义两个用public修饰的类
@Override
public void finalize() {
System.out.println("垃圾被清扫了");
}
}
import java.math.BigInteger;
public class Demo4_BigInteger {
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]);
}
}
}
A:BigDecimal的概述
所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal
不可变的、任意精度的有符号十进制数。
import java.math.BigDecimal;
public class Demo5_BigDecimal {
public static void main(String[] args) {
//System.out.println(2.0 - 1.1);
/*BigDecimal bd1 = new BigDecimal(2.0); //这种方式在开发中不推荐,因为不够精确
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));*/
BigDecimal bd1 = BigDecimal.valueOf(2.0); //这种方式在开发中也是推荐的
BigDecimal bd2 = BigDecimal.valueOf(1.1);
System.out.println(bd1.subtract(bd2));
}
}
import java.util.Date;
public class Demo6_Date {
public static void main(String[] args) {
//demo1();
//demo2();
Date d1 = new Date();
d1.setTime(1000); //设置毫秒值,改变时间对象
System.out.println(d1);
}
public static void demo2() {
Date d1 = new Date();
System.out.println(d1.getTime()); //通过时间对象获取毫秒值
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); //通过毫秒值创建时间对象
}
}
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo7_SimpleDateFormat {
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);
}
public static void demo3() {
Date d = new Date(); //获取当前时间对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//创建日期格式化类对象
System.out.println(sdf.format(d)); //将日期对象转换为字符串
}
public static void demo2() {
Date d = new Date(); //获取当前时间对象
SimpleDateFormat sdf = new SimpleDateFormat(); //创建日期格式化类对象
System.out.println(sdf.format(d)); //88-6-6 下午9:31
}
public static void demo1() {
//DateFormat df = new DateFormat(); //DateFormat是抽象类,不允许实例化
//DateFormat df1 = new SimpleDateFormat();
DateFormat df1 = DateFormat.getDateInstance(); //相当于父类引用指向子类对象,右边的方法返回一个子类对象
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test2 {
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 );
}
}
import java.util.Calendar;
public class Demo8_Calendar {
public static void main(String[] args) {
Calendar c = Calendar.getInstance(); //父类引用指向子类对象
//System.out.println(c); //证明重写了toString方法打印了对象中所有的属性
System.out.println(c.get(Calendar.YEAR)); //通过字段获取对应的值
System.out.println(c.get(Calendar.MONTH));
}
}
import java.util.Calendar;
public class Demo9_Calendar {
public static void main(String[] args) {
//demo1();
Calendar c = Calendar.getInstance(); //父类引用指向子类对象
//c.add(Calendar.MONTH, -1); //对指定的字段进行向前减或向后加
//c.set(Calendar.YEAR, 2000); //修改指定字段
c.set(2000, 7, 8);
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)));
}
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开始编号的
System.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 = {"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
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;
}
}
import java.util.Calendar;
import java.util.Scanner;
public class Test3 {
/**
* * A:案例演示
* 需求:键盘录入任意一个年份,判断该年是闰年还是平年
* Calendar c = Calendar.getInstance();
*
* 分析:
* 1,键盘录入年Scanner
* 2,创建Calendar c = Calendar.getInstance();
* 3,通过set方法设置为那一年的3月1日
* 4,将日向前减去1
* 5,判断日是多少天,如果是29天返回true否则返回false
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份,判断该年份是闰年还是平年:");
//int year = sc.nextInt();
String 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);
//将日向前减去1
c.add(Calendar.DAY_OF_MONTH, -1);
//判断是否是29天
return c.get(Calendar.DAY_OF_MONTH) == 29;
}
}