JAVA学习笔记-----Thirteen(正则表达式,Math)

一.正则表达式

1.正则表达式的概述
A:正则表达式:正确规则的表达式 规则java给我们定的
是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。
2.正则表达式的组成规则
规则字符在java.util.regex Pattern类中
A:字符
x 字符 x。举例:‘a’表示字符a
\ 反斜线字符。
\n 新行(换行)符 (’\u000A’)
\r 回车符 (’\u000D’)
B:字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
[0-9] 0到9的字符都包括
C:预定义字符类
. 任何字符。我的就是.字符本身,怎么表示呢? .
\d 数字:[0-9]
\w 单词字符:[a-zA-Z_0-9]
在正则表达式里面组成单词的东西必须有这些东西组成
D:边界匹配器
^ 行的开头
$ 行的结尾
\b 单词边界
就是不是单词字符的地方。
举例:hello world?haha;xixi
E:Greedy 数量词
X? X,一次或一次也没有 比如""空串 就是没有
X* X,零次或多次 大于等于1次 都算多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次
3.正则表达式的判断功能
String类的功能:public boolean matches(String regex)
4.正则表达式的分割功能
String类的功能:public String[] split(String regex)
5.正则表达式的替换功能
String类的功能:public String replaceAll(String regex,String replacement)
6. Pattern和Matcher的概述
正则的获取功能需要使用的类

A:Pattern和Matcher的概述
B:模式和匹配器的典型调用顺序
通过JDK提供的API,查看Pattern类的说明
典型的调用顺序是
Pattern p = Pattern.compile(“a*b”);
Matcher m = p.matcher(“aaaaab”);
boolean b = m.matches();
7. 正则表达式的获取功能
Pattern和Matcher的结合使用
案例演示:

public static void main(String[] args) {
        String regx="[0-9]+"; //可以出现一个或者多个
        regx="[a-z]?";//出现一次,或一次也不要出现  "" 空串就是没有出现
        regx="[A-Z0-9]*";//零次或多次 一次也算多次
        regx="[a-z]{3}"; // 恰好几次
        regx="[0-9]{3,}";// 至少几次
        regx="[a-z0-9A-Z]{6,12}"; //等于等于 6 小于等12

        String regex = "[1-9][0-9]{4,14}";

        boolean b = "12300abc".matches(regx);
        System.out.println(b);
    }

  //邮箱的正则
        //xibuk242424aiyuan@163.com
        // 284209510@qq.com
        // xibukaiyuan@163.net
        //xibukaiyuan@163.cn
        //6 ~18 个字符,可使用字母、数字、下划线,需以字母开头
        String emailRegx = "[1-9a-zA-Z][0-9a-z_A-Z]{5,17}@[1-9a-z]{2,10}\\.(com|cn|net|org)";


        boolean matches = "xibuk2aiyuan@163.com".matches(emailRegx);
        System.out.println(matches);

二.Math类

A:Math类概述
Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。
B: 成员变量
public static final double E : 自然底数
public static final double PI: 圆周率
C:成员方法
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) 获取最大值
public static int min(int a, int b) 获取最小值
public static double pow(double a,double b) 获取a的b次幂
public static double random() 获取随机数 返回带正号的 double 值,该值大于等于 0.0 且小于 1.0。
public static int round(float a) 四舍五入
public static double sqrt(double a)获取正平方根
案例演示:

    public static void main(String[] args) {
        int[] arr={10,20,30,40,50,60,70,80,90,100};
        //删除一个数组中 1索引处的元素
        int index=5;
        int[] tempArr = new int[arr.length - 1];

        for (int i = 0; i < tempArr.length; i++) {
            if(i<index){
                tempArr[i]=arr[i];
            }else{
                tempArr[i]=arr[i+1];
            }
        }

        arr=tempArr;
        System.out.println(Arrays.toString(arr));
    }
三.Random类

A:Random类的概述
此类用于产生随机数如果用相同的种子创建两个 Random 实例,
则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列。
B:构造方法
public Random() 没有给定种子,使用的是默认的(当前系统的毫秒值)
public Random(long seed) 给定一个long类型的种子,给定以后每一次生成的随机数是相同的
C:成员方法
public int nextInt()//没有参数 表示的随机数范围 是int类型的范围
public int nextInt(int n)//可以指定一个随机数范围

案例演示:

 //使用单个 long 种子创建一个新的随机数生成器。
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            System.out.println(random.nextInt(100));
        }

四.System类

A:System类的概述
System 类包含一些有用的类字段和方法。它不能被实例化。
B:成员方法
public static void gc()//调用垃圾回收器
public static void exit(int status)//退出java虚拟机 0 为正常退出 非0为 异常退出
public static long currentTimeMillis()//获取当前时间的毫秒值

  long start = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            System.out.println(1);
        }
        long end = System.currentTimeMillis();

        System.out.println("耗时:"+(end-start)+"毫秒");

五.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)//除法
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)//scale 小数点后面保留几位
// roundingMode 取舍模式 比如四舍五入

 BigInteger a = new BigInteger(Integer.MAX_VALUE + "");
        BigInteger b = new BigInteger(Integer.MAX_VALUE + "");
        BigInteger multiply = a.multiply(b);
        System.out.println(multiply);

六.Date类

A:Date类的概述
类 Date 表示特定的瞬间,精确到毫秒。
B:构造方法
public Date()
public Date(long date) //把一个long类型的毫秒值转换成一个日期对象
C:成员方法
public long getTime(): 获取一个日期对象对象毫秒值
public void setTime(long time): 给一个日期对象设置上指定的毫秒值 例:date.setTime(1000 * 60 * 60) ;

七.SimpleDateFormat类

A.SimpleDateFormat类的概述
SimpleDateFormat: 可以把一个日期对象格式化成一个文本(字符串) , 也可以把一个日期字符串解析成一个日期对象
B. 构造方法:
public SimpleDateFormat():使用默认的模式来创建一个SimpleDateFormat对象
public SimpleDateFormat(String pattern):使用指定的模式(规则比如yyyy:MM:dd HH:mm:ss)来创建一个SimpleDateFormat对象
C.规则的定义
y 年
M 月
d 天
H 时
m 分
s 秒
D.成员方法:
public String format(Date date): 把一个日期对象格式化成一个字符串
public Date parse(String dateStr): 把一个日期字符串解析成一个日期对象 注意要以指定格式解析

八.Calendar类

A:Calendar类的概述
Calendar 类是一个抽象类,不能直接new对象,可以通过他的一个静态成员方法getInstance()来获取他的对象
它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR
等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。
B:成员方法
public static Calendar getInstance() 使用默认时区和语言环境获得一个日历对象
public int get(int field) 获得给定日历字段对应的值 field通过Calendar提供的字段来拿

九. Calendar类的add()和set()方法

A:成员方法
public void add(int field,int amount) 根据日历的规则,为给定的日历字段添加或减去指定的时间量
public final void set(int year,int month,int date) 设置日历时间 年月日

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值