正则以及杂七杂八的类(math,random,system,BigDecimal,Date,SimpleDateFormat,Calendar)

首先介绍排序

快速排序

public class Text2{
    public static void  kuaipai(int arr[],int statrt,int end) {
        if (statrt < end) {
            int zhon = zhong(arr, statrt, end);
            kuaipai(arr, statrt, zhon - 1);
            kuaipai(arr, zhon + 1, end);
        }
    }
        private static int zhong( int arr[], int statrt, int end){
            int a = statrt;
            int b = end;
            int x = arr[a];
            while (a < b) {
                while (a < b && arr[b] > x) {
                    b--;
                }
                if (a < b && arr[b] < x) {
                    arr[a] = arr[b];
                    a++;
                }
                while (a < b && arr[a] <= x) {
                    a++;
                }
                if (a < b && arr[a] > x) {
                    arr[b] = arr[a];
                    b--;
                }
                arr[a] = x;
            }
            return a;
        }
    }

测试区

public class Zuo {
    public static void main(String[] args) {
        int[]arr={3,45,13,56,78,24,32,14,32};
        Text2.kuaipai(arr,0,arr.length-1);
        System.out.println(Arrays.toString(arr));
    }
}

接下来介绍正则表达式
它是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。

正则表达式的组成规则

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 次

例子如下:
/**
* 手机号码的规则
* 都是1开头
* 都是11 位
*
*/```
public class Text2 {
public static void main(String[] args) {
String telephone=“134496735”;
String phoneRegx="[1][3489][0-9]{9}";
boolean b = telephone.matches(phoneRegx);
System.out.println(b);
}
}


## 正则表达式的分割功能

  String str="aaa-bbb-ccc";
    String[] strArr = str.split("-");

    System.out.println(Arrays.toString(strArr));

    String str2 = "aaa-1232424bbb-7890ccc";

    String[] split = str2.split("[0-9\\-]+");
    System.out.println(Arrays.toString(split));

``

  //A:
        //案例演示:
        //需求:我有如下一个字符串:”91 27 46 38 50”,请写代码实现最终输出结果是:”27 38 46 50 91”

        String str = "91 27 46 38 50";
        //-分析:
        //-a:定义目标字符串 "91 27 46 38 50"
        //        - b:对这个字符串进行切割, 得到的就是一个字符串数组
        //        - c:把b中的字符串数组转换成int类型的数组
        //        - (1):定义一个int类型的数组, 数组的长度就是字符串数组长度
        //        - (2):遍历字符串数组, 获取每一个元素.将其转换成int类型的数据
        //        - (3):把int类型的数据添加到int类型的数组的指定位置
        //        - d:排序
        //        - e:创建一个StringBuilder对象, 用来记录拼接的结果
        //        - f:遍历int类型的数组, 将其每一个元素添加到StringBuilder对象中
        //        - g:就是把StringBuilder转换成String
        //        - h:输出
        //"27 38 46 50 91";
        String[] strings = str.split(" ");
        //遍历字符串数组,取出元素,装到一个int数组里面去
        int[] arr = new int[strings.length];
        for (int i = 0; i < strings.length; i++) {
            arr[i] = Integer.parseInt(strings[i]);
        }

        //排序
        Arrays.sort(arr);

        // System.out.println(Arrays.toString(arr));
        //遍历int数组拼串
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]).append(" ");
        }
        String string = sb.toString().trim();
        System.out.println(string);

    }

正则表达式的功能

正则表达式的替换功能

就如同String中的一样用replace,只不过这里替换的字符串可以用正则表达式来代替

Pattern和Matcher的概述

正则的获取功能需要使用的类:Pattern和Matcher
典型的调用顺序是
Pattern p = Pattern.compile(“a*b”);
Matcher m = p.matcher(“aaaaab”);
boolean b = m.matches();

正则表达式的获取功能

Pattern和Matcher的结合使用

  String regx="\\b[a-z]{3}\\b";
        String str="da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
        Pattern p = Pattern.compile(regx);
        Matcher m = p.matcher(str);
        while (m.find()){
            String group = m.group();
            System.out.println(group);
        }

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)获取正平方根

Random类的概述和方法使用

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

System类的概述和方法使用

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

BigDecimal类的概述和方法使用

A:BigDecimal的概述
由于在运算的时候,float类型和double很容易丢失精度。
所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal不可变的、任意精度的有符号十进制数。
可以让超过Integer范围内的数据进行运算。
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 取舍模式 比如四舍五入

Date类的概述和方法使用

A:Date类的概述
类 Date 表示特定的瞬间,精确到毫秒。
B:构造方法
public Date()
public Date(long date) //把一个long类型的毫秒值转换成一个日期对象
Date ---- long 的转换
调用getTime方法
long — Date 的转换
可以使用构造方法
setTime(long time)

SimpleDateFormat类实现日期和字符串的相互转换

Date类的好多方法都已过期并由此类代替。
SimpleDateFormat: 可以把一个日期对象格式化成一个文本(字符串) , 也可以把一个日期字符串解析成一个日期对象

  • 构造方法:
  • public SimpleDateFormat():使用默认的模式来创建一个SimpleDateFormat对象
  • public SimpleDateFormat(String pattern):使用指定的模式(规则比如yyyy:MM:dd HH:mm:ss)来创建一个SimpleDateFormat对象

规则的定义

y 年
M 月
d 天
H 时
m 分
s 秒

成员方法:
public String format(Date date): 把一个日期对象格式化成一个字符串
public Date parse(String dateStr): 把一个日期字符串解析成一个日期对象 注意要以指定格式解析
它就像是Date类和String类之间的一座桥梁,不管你想从哪方转换,都可以先转成SimpleDateFormate()类在调用方法进行转换。
日期转String

 SimpleDateFormat format = new SimpleDateFormat();
        //format(date) 把一个日期对象,格式化成一个日期字符串。
        String time = format.format(date);
        System.out.println(time);

String转日期

  String dateStr="2019年04-24 13:42:00";
        //ParseException 解析失败异常
        //解析字符串的格式,和我们指定的格式要一致。
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM-dd HH:mm:ss");
        //把日期字符串,解析成日期对象
        Date date = dateFormat.parse(dateStr);
        System.out.println(date);

这里要注意格式的书写一致,即String类的格式要跟SimpleDateFormat的格式保持一致。
例子如下:

   Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的生日 格式 1990-12-12");
        String s = sc.nextLine();
        Date date = DateUtils.parseDate(s, "yyyy-MM-dd");
        long time = date.getTime();//你出生的那天的毫秒值
        long end = System.currentTimeMillis();


        System.out.println("来到这世界"+((end-time)/1000/60/60/24/365)+"年");
 private DateUtils() {
    }
    // 把日期格式化成字符串
    public static String formatDate(String format) {
        SimpleDateFormat format1 = new SimpleDateFormat(format);
        String dateTime = format1.format(new Date());
        return dateTime;
    }
    // 把字符串解析成日期

    public static Date parseDate(String dateStr, String format) throws ParseException {
        SimpleDateFormat format1 = new SimpleDateFormat(format);
        Date date = format1.parse(dateStr);
        return date;
    }
}

Calendar类的概述和获取日期的方法

Calendar 类是一个抽象类,不能直接new对象,可以通过他的一个静态成员方法getInstance()来获取他的对象
它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

 Calendar instance = Calendar.getInstance();
        instance.set(2019,11,16);
        System.out.println(instance.get(Calendar.DAY_OF_MONTH));
        System.out.println(instance.get(Calendar.DAY_OF_WEEK));
 //需求:键盘录入任意一个年份,获取任意一年的二月有多少天
        //分析:
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个年份");
        int year = scanner.nextInt();
        LocalDate of = LocalDate.of(2019, 1, 1);
        boolean b = of.isLeapYear();//判断是否是闰年
        System.out.println(b);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值