### 14.01_ common objects (regular expressions and simple overview of the use)
- A: Regular Expressions
- It refers to a string used to describe a single or series of line matching a string of grammar rules. It is actually a rule. It has its own special applications.
- Role: for example, registered mail, mail user name and password, usually to limit the length of this thing is to limit the length of the regular expression to do
- B: Case presentation
- Requirements: check qq number.
- 1: requirements must be 5-15 digits
- 2: 0 at the beginning can not
- 3: must all be digital
- a: Non-Regular Expression
- b: Regular Expression
### 14.02_ common objects (character class presentation)
- A: Character Classes
- [Abc] a, b, or C (Simple Profile)
- [^ Abc] any character, in addition to a, b or C (negative)
- [A-zA-Z] a to z or A to Z, including two letters (range)
- Character [0-9] comprises 0 to 9
### 14.03_ common objects (predefined character classes demo)
- A: Predefined character classes
- Any characters.
- \ D figures: [0-9]
- \ W word character: [a-zA-Z_0-9]
### 14.04_ common objects (quantifier)
- A: Greedy quantifiers
- X? X, once or not even once
- X * X, zero or more times
- X + X, one or more times
- X {n} X, exactly n times
- X {n,} X, at least n times
- X {n, m} X, at least n times, but not more than m times
### 14.05_ common objects (regular expression splitting function)
- A: regular expression split function
- String class functions: public String [] split (String regex)
- B: Case presentation
- Regular expressions split function
public class Demo5_Split {
/**
* @param args
*/
public static void main(String[] args) {
String s = "ccc.mmm.ttt";
String[] arr = s.split("\\."); //通过正则表达式切割字符串
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("11111111111111111");
}
}
### 14.06_ common objects (the given numbers sorted string)
- A: Case presentation
- Demand: I have the following string: "9127463850", please write code to achieve the final result is output: "2738465091"
import java.util.Arrays;
public class Test1 {
/**
* @param args
* * A:案例演示
* 需求:我有如下一个字符串:”91 27 46 38 50”,请写代码实现最终输出结果是:”27 38 46 50 91”
* 100
* 80
* 分析:
* 1,将字符串切割成字符串数组
* 2,将字符串转换成数字并将其存储在一个等长度的int数组中
* 3,排序
* 4,将排序后的结果遍历并拼接成一个字符串
*/
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_ common objects (regular expression replacement function)
- A: Regular expression replacement function
- String class functions: public String replaceAll (String regex, String replacement)
- B: Case presentation
- Regular expression substitution function
public class Main {
public static void main(String[] args){
String s = "c1m2t3";
String regex = "\\d"; //\\d代表的是任意数字
String s2 = s.replaceAll(regex,"");
System.out.println(s2);
}
}
### 14.08_ common objects (regular expression grouping function)
- A: Regular expression grouping function
- Capture group which may be opened by the bracket numbered left to right. For example, in the expression ((A) (B ©)), there are four such groups:
-
1 ((A)(B(C))) 2 (A 3 (B(C)) 4 (C) 组零始终代表整个表达式。
-
B: Case presentation
- a: Cutting
- Required: Please cut to fold word: "sdqqfgkkkhjppppkl";
- b: Replace
- Requirements: I ... I ... I ... I want to ... to learn ... to learn ... learn knitting ... compiled code programming ... Cheng Cheng Cheng.....
- Restore string into: "I want to learn programming."
- Case number one:
public class Main {
public static void main(String[] args){
//叠词 快快乐乐,高高兴兴
String regex = "(.)\\1(.)\\2";
//\\1代表第一组又出现一次,\\2代表第二组又出现一次
System.out.println("快快乐乐".matches(regex)); //true
System.out.println("快乐乐乐".matches(regex)); //false
}
}
- Case II:
public class Main {
public static void main(String[] args){
//叠词 快快乐乐,高高兴兴
String regex = "(..)\\1"; //\\1代表第一组又出现一次
System.out.println("高兴高兴".matches(regex)); //true
System.out.println("快乐乐乐".matches(regex)); //false
}
}
- Case 3: cutting
public class the Main {
public static void main (String [] args) {
String S = "sdqqfgkkkhjppppkl";
String REGEX = "(.) \ 1+"; // \ 1+ to represent the first group appears once repeatedly
String [] = ARR s.split (REGEX);
for (int I = 0; I <arr.length; I ++) {
System.out.println (ARR [I]);
}
}
}
- Case Four: Replace
public class Main {
public static void main(String[] args){
String s = "我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程";
//不能使用.因为点代表任意字符,要用\\.,后面使用+代表出现一次到多次
String s2 = s.replaceAll("\\.+","");
System.out.println(s2);
String s3 = s2.replaceAll("(.)\\1+","$1");
//第二个参数代表获取到第一个组的数据把前面第一个参数的内容的替换掉,$1代表第一组中的内容
System.out.println(s3); //我要学编程
}
}
### 14.09_ common objects (Overview of the Pattern and Matcher)
- A: Overview of the Pattern and Matcher
- A typical call sequence and pattern matcher: B
- API provided by JDK, see a description of the Pattern class
- A typical call sequence is
- Pattern p = Pattern.compile(“a*b”);
- Matches m = p.matcher ( "aaaaab");
- boolean b = m.matches();
public class Main {
public static void main(String[] args){
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.10_ common objects (regular expression acquisition function)
- A: regular expressions acquisition function
- Pattern Matcher and combined use
- B: Case presentation
- Requirements: a string of cell phone numbers get out
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args){
String s = "我的手机号码是18437395028,曾经用过18936739581,还用过13465464328";
String regex = "1[3578]\\d{9}"; //手机号码的正则表达式
// Pattern p = Pattern.compile(regex);
// Matcher m = p.matcher(s);
// boolean b = m.matches();
// System.out.println(b); //false
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
// boolean b1 = m.find(); //find方法为找到子序列
// System.out.println(b1); //true
// String s1 = m.group(); //group方法为获取子序列
// System.out.println(s1); //18437395028
//用循环找多个电话号码
while (m.find())
System.out.println(m.group());
// 18437395028
// 18936739581
// 13465464328
}
}
### 14.11_ common objects (Math class overview and the methods used)
- A: Math Class Overview
- Math class contains methods for performing basic mathematical operations, like ever exponential, logarithmic, trigonometric functions and square root.
- B: member method
- public static int abs(int a)
- public static double ceil(double a)
- public static double floor(double a)
- public static int max (int a, int b) min Self
- public static double pow(double a,double b)
- public static double random()
- public static int round (float a) is a double self-parameter
- public static double sqrt(double a)
public class Main {
public static void main(String[] args){
System.out.println(Math.PI); //3.141592653589793
System.out.println(Math.abs(-10));//取绝对值10
System.out.println(Math.ceil(12.3));//向上取整但结果为double值13.0
System.out.println(Math.ceil(12.99));//向上取整但结果为double值13.0
System.out.println(Math.floor(12.3));//向下取整但结果为double值12.0
System.out.println(Math.floor(12.99));//向下取整但结果为double值12.0
System.out.println(Math.max(2,4));//获取最大值4
System.out.println(Math.pow(2,3));//前面的数为底数,后面为指数,2的3次方8
System.out.println(Math.random());//生成0.0-1.0之间的随机小数,包括0.0,不包括1.0
System.out.println(Math.round(12.3f));//四舍五入12
System.out.println(Math.round(12.5f));//四舍五入13
System.out.println(Math.sqrt(4));//开平方2
}
}
### 14.12_ common objects (Random class overview and method of use)
- Random class Overview General: A
- Such for generating pseudo-random numbers if you create two Random seed with the same example,
- The method is the same for each instance of the call sequence, which will generate and return the same sequence of numbers.
- B: Constructor
- public Random()
- public Random(long seed)
- C: member method
- public int nextInt ()
- public int nextInt (int n) (master key)
public class Main {
public static void main(String[] args){
Random r = new Random();
int x = r.nextInt();
int x1 = r.nextInt(100);//0-99之间的伪随机数
System.out.println(x);
System.out.println(x1);
}
}
### 14.13_ common objects (Overview and methods of use of the System class)
- Overview of System class General: A
- System class contains several useful class fields and methods. It can not be instantiated.
- B: member method
- public static void gc () // prompt garbage
- public static void exit(int status)
- public static long currentTimeMillis () // current number of milliseconds
- pubiic static void arraycopy (Object src, int srcPos, Object dest, int destPos, int length) // Array copy (original array, the array of the original start position, the target array, the starting position of the target array, a length to be copied)
- C: Case presentation
- System class member method of use
### 14.14_ common objects (Overview and methods of using BigInteger class)
- A: Overview of BigInteger
- It allows data to be within the range of more than Integer operations
- B: Constructor
- public BigInteger(String val)
- C: member method
- public BigInteger add(BigInteger val) +
- public BigInteger subtract(BigInteger val) -
- public BigInteger multiply(BigInteger val) *
- public BigInteger divide(BigInteger val) /
- Array public BigInteger [] divideAndRemainder (BigInteger val) manufactures and mold
import java.math.BigInteger;
public class Main {
public static void main(String[] args){
BigInteger b1 = new BigInteger("100");
BigInteger b2 = new BigInteger("4");
System.out.println(b1.add(b2)); //加 104
System.out.println(b1.subtract(b2)); //减 96
System.out.println(b1.multiply(b2)); //乘 400
System.out.println(b1.divide(b2)); //除 25
BigInteger[] arr = b1.divideAndRemainder(b2);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]); //除数和余数 25 0
}
}
}
### 14.15_ common objects (Overview and methods of use BigDecimal class)
- A: Overview of BigDecimal
- Since the time of the operation, float type and double precision can easily be lost, presentation cases.
- So, in order to accurately represent, floating point computing, Java provides BigDecimal
- Immutable, arbitrary-precision signed decimal numbers.
- B: Constructor
- public BigDecimal(String val)
- C: member method
- public BigDecimal add(BigDecimal augend)
- public BigDecimal subtract(BigDecimal subtrahend)
- public BigDecimal multiply(BigDecimal multiplicand)
- public BigDecimal divide(BigDecimal divisor)
- D: Case presentation
- BigDecimal class constructor method and members
import java.math.BigDecimal;
public class Main {
public static void main(String[] args){
System.out.println(2.0-1.1); //0.8999999999999999
//因为计算机存储的二进制数不能准确表示小数,只能无限接近小数
BigDecimal bd1 = new BigDecimal("2.0");
//通过构造中传入字符串的方式,开发时推荐
BigDecimal bd2 = new BigDecimal("1.1");
System.out.println(bd1.subtract(bd2)); //0.9
BigDecimal bd3 = BigDecimal.valueOf(2.0);
//这种方式在开发中也是推荐的
BigDecimal bd4 = BigDecimal.valueOf(1.1);
System.out.println(bd3.subtract(bd4)); //0.9
}
}
### 14.16_ common objects (Overview and methods of the Date class use) (master)
- Date class Overview General: A
- Date class represents a specific moment, with millisecond precision.
- Util Date class is the package, the package can not be imported sql.
- B: Constructor
- public Date()
- public Date(long date)
- C: member method
- public long getTime()
- public void setTime(long time)
import java.util.Date;
public class Main {
public static void main(String[] args){
Date d1 = new Date();
System.out.println(d1);
//Sun Oct 06 18:08:20 CST 2019
Date d2 = new Date(0);
System.out.println(d2);
//Thu Jan 01 08:00:00 CST 1970
//如果参数传为0代表1970年1月1日,如果没传参数则是当前时间
//其实系统时间依然是0点,只不过电脑时间设置为东8区,故打印结果是8点
System.out.println(d1.getTime());
//通过时间对象获取毫秒值
System.out.println(System.currentTimeMillis());
//通过系统类的方法获取当前时间毫秒值
Date d3 = new Date();
d3.setTime(1000); //设置毫秒值
System.out.println(d3);
//Thu Jan 01 08:00:01 CST 1970 因为1000毫秒等于1秒
}
}
### 14.17_ common objects (SimpleDateFormat class implements mutual conversion date and strings) (master)
- SUMMARY DateFormat class General: A
- DateFormat date / time formatting subclasses of the abstract class does not allow instantiation , which formats and parses dates or time in a language-independent manner. Is an abstract class, subclass using SimpleDateFormat
- B: SimpleDateFormat constructor
- public SimpleDateFormat()
- public SimpleDateFormat(String pattern)
- C: member method
- public final String format(Date date)
- public Date parse(String source)
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args){
//DateFormat df = new DateFormat(); //DateFormat是抽象类不能实例化
//DateFormat df1 = new SimpleDateFormat();
//DateFormat df1 = DateFormat.getDateInstance();
//相当于父类引用指向子类对象,右边的方法返回一个子类对象
Date d = new Date();//获取当前时间对象
SimpleDateFormat sdf = new SimpleDateFormat();//创建日期格式化类对象
System.out.println(sdf.format(d)); //19-10-6 下午6:36
Date d1 = new Date();//获取当前时间对象
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
//创建日期格式化类对象
System.out.println(sdf1.format(d1)); //2019年10月06日18:39:50
//将时间字符串转换为日期对象
String str = "2000年08月08日08时08分08秒";
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
Date d2 = sdf2.parse(str); //将时间字符串转换为日期对象 parse需要抛出异常
System.out.println(d);
}
}
### 14.18_ common objects (in this case how many days you come to the World) (mastered)
- A: Case presentation
- Requirements: You came to this world to count how many days?
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 1.将生日字符串和时间字符串存储在String类型的变量中
* 2.定义日期格式化对象
* 3.将日期字符串转换为日期对象
* 4.通过日期对象获取时间毫秒值
* 5.将两个时间毫秒值相减除以1000得到秒,再除60得到分,再除60得到时,再除24得到天
*/
public class Main {
public static void main(String[] args) {
//1.将生日字符串和时间字符串存储在String类型的变量中
String birthday = "1997年05月20日";
String today = "2019年10月06日";
//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.将两个时间毫秒值相减得到天
System.out.println(time/1000/60/60/24);
}
}
### 14.19_ common objects (date overview and method of obtaining the Calendar class) (master)
- Calendar class Overview General: A
- Calendar class is an abstract class that provides a specific instant and the conversion between a group such as a calendar field YEAR, MONTH, DAY_OF_MONTH, HOUR some other method, and for manipulating the calendar fields (e.g., date of obtaining the next week) provided some method.
- B: member method
- public static Calendar getInstance()
- public int get(int field)
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar c = Calendar.getInstance(); //父类引用指向子类对象
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)+"年"+ (c.get(Calendar.MONTH)+1)+"月"
+c.get(Calendar.DAY_OF_MONTH)+"日"+getWeek(c.get(Calendar.DAY_OF_WEEK)));
//2019年10月6日星期日
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)));
//2019年10月06日星期日
}
/**
* 将星期存储表中进行查表
* 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;
}
}
### 14.20_ common objects (add Calendar class () and set () method) (master)
- A: member method
- public void add(int field,int amount)
- public final void set(int year,int month,int date)
- B: Case Presentation 1
- Calendar class member method of use 1
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar c = Calendar.getInstance(); //父类引用指向子类对象
c.add(Calendar.MONTH,-1);//给指定字段赋值为两参数之和
System.out.println(c.get(Calendar.YEAR)+"年"+ (c.get(Calendar.MONTH)+1)+"月"
+c.get(Calendar.DAY_OF_MONTH)+"日");
//2019年9月6日星期日
}
}
- C: Case presentation 2
- The method of using a class member Calendar 2
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar c = Calendar.getInstance(); //父类引用指向子类对象
//c.set(Calendar.MONTH,2006);//修改指定字段 2019年9月6日星期日
c.set(2006,8,9);//修改指定字段 2006年9月9日
System.out.println(c.get(Calendar.YEAR)+"年"+ (c.get(Calendar.MONTH)+1)+"月"
+c.get(Calendar.DAY_OF_MONTH)+"日");
}
}
### 14.21_ common objects (how to obtain a common year or any year is a leap year) (master)
- A: Case presentation
- Requirements: keyboard input any one year, or to determine the average year is a leap year
import java.util.Calendar;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份,判断是闰年还是平年");
String line = sc.nextLine();
//录入数字字符串
int year = Integer.parseInt(line);
//将数字字符串转换为数字
boolean b = getYear(year);
System.out.println(b);
}
private static boolean getYear(int year){
Calendar c = Calendar.getInstance();
c.set(year,2,1);
c.add(Calendar.DAY_OF_MONTH,-1);
return c.get(Calendar.DAY_OF_MONTH) == 29;
}
}