Java 常用类

正则表达式 Pattern类

含义:用来描述或者匹配一系列符合某个语句规则的字符串

Pattern

代表正则表达式的匹配模式

Matcher

提供了对正则表达式的分组支持,以及对正则表达式的多次匹配支持

案例

案例1:把一个字符串中的电话号码替换成130****1111

public static void main(String[] args) {
    
       String str = "小明13152456666,小红13428793333";

       String regex = "(\\d{3})(\\d{4})(\\d{4})";
       //		String replaceAll = str.replaceAll(regex, "$1****$3");

       //底层原理:
       Pattern pattern = Pattern.compile(regex);//获取正则表达式的对象
       Matcher matcher = pattern.matcher(str);//匹配结果
       String replaceAll = matcher.replaceAll("$1****$2");

       System.out.println(replaceAll);
}

案例2:校验QQ邮箱

public static void main(String[] args) {
    /**
		 * 案例2:校验QQ邮箱
		 */
    String str = "1235155421@qq.com";

    //正则表达式的字符串
    String regex = "\\d{5,10}@qq.com";

    //boolean matches = str.matches(regex);
    //底层原理:
    Pattern pattern = Pattern.compile(regex);//获取正则表达式的对象
    Matcher matcher = pattern.matcher(str);//获取匹配结果
    boolean matches = matcher.matches();//判断是否完全匹配

    System.out.println(matches);
}

案例3:分割路径,通过指定符号分割路径

public static void main(String[] args) {
    
       String str = "C:\\Users\\ZHC\\Pictures\\头像.jpg";

       //正则表达式的字符串
       String regex = ":?\\\\";

       String[] split = str.split(regex);
       //底层原理
       //Pattern pattern = Pattern.compile(regex);//获取正则表达式的对象
       //String[] split = pattern.split(str);//分隔

       for (String string : split) {
           System.out.println(string);
       }
}

案例4:Pattern+Matcher 找到前端代码中的图片路径

public static void main(String[] args) {
    /**
	案例4:Pattern+Matcher 找到前端代码中的图片路径	
	*/

       String str = "<img src='hhy/aaa.jpg'/><div><div/> <input type='image' src='submit.gif' /><img src='bbb.jpg'/>";

       //正则表达式的字符串
       String regex = "<img\\b[^>]*\\bsrc\\b\\s*=\\s*('|\")?([^'\"\n\r\f>]+(\\.jpg)\\b)[^>]*>";

       //获取正则表达式对象
       Pattern pattern = Pattern.compile(regex);
       //获取匹配结果的对象
       Matcher matcher = pattern.matcher(str);

       //System.out.println("在字符串中是否整个匹配:" + matcher.matches());
       //System.out.println("在字符串中是否开头就匹配:" + matcher.lookingAt());
       //System.out.println("在字符串中是否有包含匹配:" + matcher.find());

       //遍历查找
       while(matcher.find()){
           String group = matcher.group(2);//获取匹配结果
           System.out.println(group);
           //hhy/aaa.jpg
           //bbb.jpg
       }
}

总结

  1. Pattern与Matcher一起合作
  2. Matcher类提供了对正则表达式的分组支持,以及对正则表达式的多次匹配支持.
  3. 单独用Pattern只能使用Pattern.matches(String regex,CharSequence input),是一种最基础最简单的匹配。

经验:

  1. 正则表达式在工作中实际应用在验证邮箱、验证手机号码、替换字符串
  2. 正则表达式几乎不用我们自己写,百度即可

关于日期时间的类

  1. Date 日期类
  2. SimpleDateFormat 格式化日期类
  3. Calendar 日历类
Date 类
public static void main(String[] args) {
		
       Date date = new Date();
       //星期 月份日期 时:分:秒 时区 年份
       //Thu Aug 05 11:23:00 CST 2021
       System.out.println(date);

       //自1970.1.1 0:0:0 往后推1000毫秒的时间
       //Date date = new Date(1000);
       //Thu Jan 01 08:00:01 CST 1970
       //System.out.println(date);

}
SimpleDateFormat 类
public static void main(String[] args) throws ParseException{

       SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");	
       //将Date转为字符串
       String format = sdf.format(new Date());
       System.out.println(format);	//2021年08月05日 19:26:02

       //将字符串转为 Date
       Date date = sdf.parse(format);
       System.out.println(date);	//Thu Aug 05 19:26:02 CST 2021
}
Calendar 类
public static void main(String[] args) {

       //获取日历类对象
       Calendar calendar = Calendar.getInstance();

       //获取单个的日历信息
       int year = calendar.get(Calendar.YEAR);
       int month = calendar.get(Calendar.MONTH)+1;
       int day = calendar.get(Calendar.DAY_OF_MONTH);
       int hour = calendar.get(Calendar.HOUR_OF_DAY);
       int minute = calendar.get(Calendar.MINUTE);
       int second = calendar.get(Calendar.SECOND);

       System.out.println(year+"年"+month+"月"+day+"日 "+hour+"时"+minute+"分"+second+"秒");	//2021年8月5日 19时27分37秒

}
总结:
  1. Date+SimpleDateFormat:获取一连串的日期数据,通过format()方法格式化,转换成字符串
  2. Calendar:获取单个日期数据

Math 类

  1. Math 类提供了一序列基本数学运算和几何函数的方法。
  2. Math类是final类,并且它的所有成员变量和成员方法都是静态的。
常用方法
public static void main(String[] args) {
    
	System.out.println("求平方:" + Math.pow(3, 2));//9.0
	System.out.println("求平方根:" + Math.sqrt(9));//3.0
	System.out.println("求绝对值:" + Math.abs(-100));//100
	System.out.println("向上取整(天花板):" + Math.ceil(1.001));//2.0
	System.out.println("向下取整(地板):" + Math.floor(1.99));//1.0
	System.out.println("求最大值:" + Math.max(10, 20));//20
	System.out.println("求最小值:" + Math.min(10, 20));//10
	System.out.println("四舍五入:" + Math.round(1.4));//1
	System.out.println("获取随机值(0包含~1不包含):" + Math.random());
}
应用
public static void main(String[] args) {

    //产生1~10的随机数
    System.out.println((int)(Math.random()*10) + 1);
    System.out.println(new Random().nextInt(10)+1);
    
    //产生5~10的随机数
    System.out.println((int)(Math.random()*6)+5);
	System.out.println(new Random().nextInt(6)+5);

面试题:Math.abs() 有可能返回负数吗?
答:当数据超出int的最大值后,abs返回的就是负数(最大值加一,符号位为1,是负数)
//底层实现:
//    public static int abs(int a) {
//       return (a < 0) ? -a : a;
//    }

  System.out.println(Math.abs(Integer.MAX_VALUE+1));//-2147483648
}
静态导入

静态导入:将Math类中所有的静态属性和静态方法都导入Test03这个类中,把导入的静态属性和静态方法都认为是Test03自己的内容,可以直接调用方法

import static java.lang.Math.*;

public class Test03 {

   public static void main(String[] args) {
        int a = 10;

        System.out.println("求平方:" + pow(3, 2));//9.0
        System.out.println("求平方根:" + sqrt(9));//3.0
        System.out.println("求绝对值:" + abs(-100));//100
        System.out.println("向上取整(天花板):" + ceil(1.001));//2.0
        System.out.println("向下取整(地板):" + floor(1.99));//1.0
        System.out.println("求最大值:" + max(10, 20));//20
        System.out.println("求最小值:" + min(10, 20));//10
        System.out.println("四舍五入:" + round(1.4));//1

        //静态导入缺点:可读性不高
        //如果本类中有和静态导入类相同的方法,会就近调用本类中的方法
        System.out.println("获取随机值(0包含~1不包含):" + random());
    }

       private static int random() {
           return 123456789;
       }

}

Random 类

Random随机类的使用
public static void main(String[] args) {

    //创建随机类的对象
    Random ran = new Random();

    int nextInt1 = ran.nextInt();
    System.out.println("随机出int取值范围内的数字:" + nextInt1);

    int nextInt2 = ran.nextInt(10);
    System.out.println("随机出0~9的数字:" + nextInt2);

    boolean nextBoolean = ran.nextBoolean();
    System.out.println("随机出boolean值:" + nextBoolean);
}

需求:点名器

 //创建随机类的对象
public static void main(String[] args) {

    Random ran = new Random();
    String[] names = {"小红","小明","小强","小李","小王"};

    int index = ran.nextInt(names.length);

    System.out.println(names[index]);
}
深入Random

注意:随机全靠种子数,种子数固定,随机出的数据也是固定的

public static void main(String[] args) {

    //Random ran = new Random(10);//种子数固定,产生的数也是固定的
    Random ran = new Random();
    System.out.println(ran.nextInt());
    System.out.println(ran.nextInt(100));

    System.out.println("--------------");

    MyRandom myRandom = new MyRandom();
    System.out.println(myRandom.nextInt());
    System.out.println(myRandom.nextInt(10));
}

自定义MyRandom类,模拟Random底层实现

public class MyRandom {
	//种子数
	private long seed;

	public MyRandom() {
		//seedUniquifier() ^ System.nanoTime() 获取到相对随机的种子数
		this(seedUniquifier() ^ System.nanoTime());
	}
	//产生随机数的一个算法
	public static long seedUniquifier(){
		long current = System.currentTimeMillis();
		for(;;){
			current += current*5/2+3;
			if(current%4==0 || current%7==0){
				return current;
			}
		}
	}

	public MyRandom(long seed){
		this.seed = seed;
	}

	public int nextInt(){
		return (int) seed;
	}

    //产生0~(i-1)范围内的整数
	public int nextInt(int i){
		return Math.abs((int) seed) % i;
	}
}

Runtime 类

Runtime - 运行环境类

public static void main(String[] args) {

       //获取运行环境类的对象
       Runtime runtime = Runtime.getRuntime();

       System.out.println("获取最大内存数(字节):" + runtime.maxMemory());
       System.out.println("获取闲置内存数(字节):" + runtime.freeMemory());
       System.out.println("获取jvm虚拟机可用核心数:" + runtime.availableProcessors());
}

测试程序效率(时间、内存)

public static void main(String[] args) {

       //获取自1970.1.1 0:0:0到现在的毫秒数
       Runtime run = Runtime.getRuntime();
       long startTime = System.currentTimeMillis();
       long startMemory = run.freeMemory();
       StringBuilder sb = new StringBuilder("小明");
       for (int i = 0; i < 50000; i++) {
           sb.append("睡觉!");
       }
       long endMemory = run.freeMemory();
       long endTime = System.currentTimeMillis();
       System.out.println("消耗时长:" + (endTime-startTime));//5
       System.out.println("消耗内存(字节):" + (startMemory-endMemory));//2457664
}

System 类

System(系统类)的属性: in、out、err

public static void main(String[] args) {

       //系统标准的输入流(方向:控制台 -> 程序)
       InputStream in = System.in;

       Scanner scan = new Scanner(in);
       String next = scan.next();

       //系统标准的输出流(方向:程序 -> 控制台)
       //PrintStream out = System.out;
       //out.println(next);

       //系统标准的错误输出流(方向:程序 -> 控制台)
       PrintStream err = System.err;
       err.println(next);

       //关闭资源
       scan.close();
}
public static void main(String[] args) {
		/**
		 * 知识点:System的out和err
		 * 
		 * 理解:out和err是两个线程,谁抢到CPU资源就运行
		 * 说明:多线程程序的随机性很强
		 */
		
		System.out.println("小明");
		System.err.println("小红");
		System.out.println("小强");
	}

知识点:System的方法

public static void main(String[] args) {
    //获取系统参数的对象
    Properties properties = System.getProperties();
    //System.out.println(properties);
    //通过键获取值
    String value = System.getProperty("os.name");
    System.out.println(value);
    //退出当前虚拟机
    System.exit(0);
}

大数值运算类

BigInteger

BigInteger - 整数类型的大数值运算类

public static void main(String[] args) {
		
       BigInteger big1 = new BigInteger("123456789123456789123456789");	
       BigInteger big2 = new BigInteger("123456789123456789123456789");	

       BigInteger add = big1.add(big2);//加法
       System.out.println(add);

       BigInteger subtract = big1.subtract(big2);//减法
       System.out.println(subtract);

       BigInteger multiply = big1.multiply(big2);//乘法
       System.out.println(multiply);

       BigInteger divide = big1.divide(big2);//除法
       System.out.println(divide);

}

BigDecimal

BigDecimal - 小数类型的大数值运算类

public static void main(String[] args) {
    
       BigDecimal big1 = new BigDecimal("0.5");	
       BigDecimal big2 = new BigDecimal("0.4");	

       BigDecimal add = big1.add(big2);//加法
       System.out.println(add);

       BigDecimal subtract = big1.subtract(big2);//减法
       System.out.println(subtract);

       BigDecimal multiply = big1.multiply(big2);//乘法
       System.out.println(multiply);

       BigDecimal divide = big1.divide(big2);//除法
       System.out.println(divide);

}

注意:小数除法一定要设置保留几位小数 和 进制模式

	BigDecimal bigDecima1 = new BigDecimal("10");
       BigDecimal bigDecima2 = new BigDecimal("3");

       //除法
       BigDecimal divide = bigDecima1.divide(bigDecima2,2,BigDecimal.ROUND_HALF_UP);
       System.out.println(divide);//3.33
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值