Java基础类库

String和StringBuffer

简单的String,一旦定了就再也改不了
StringBuffer比较灵活,想从哪个改都行,序列也可以变
《疯狂java》的代码,注释后面是效果

public class StringBuilderTest
{
	public static void main(String[] args)
	{
		StringBuilder sb = new StringBuilder();
	     //开了个叫sb的字符
		sb.append("java");//sb = "java"
		sb.insert(0 , "hello "); // sb="hello java"
		sb.replace(5, 6, ","); // sb="hello,java"
		sb.delete(5, 6); // sb="hellojava"
		System.out.println(sb);

		sb.reverse(); //abc变成cba
		System.out.println(sb);
		System.out.println(sb.length());//abc长度为3
		System.out.println(sb.capacity()); //

		sb.setLength(5); // sb="avajo"
		System.out.println(sb);
	}
}

Math

一系列的数学运算

--下面是三角运算--
1/将弧度转换成角度
System.out.println ("Math.toDegrees (1.57):"
+ Math.toDegrees(1.57));
//将角度转换为弧度
System.out.println ( "Math.toRadians (90):"
+ Math.toRadians (90));
//计算反余弦,返回的角度范围在0.0到pi之间
System.out.println("Math.acos(1.2):"+ Math.acos (1.2));
//计算反正弦,返回的角度范围在-pi/2到 pi/2之间
System.out.println ( "Math.asin (0.8): " + Math.asin(0.8));//计算反正切,返回的角度范围在-pi/2到 pi/2之间
System.out .println("Math.atan (2.3):" + Math.atan (2.3));//计算三角余弦
System.out.println("Math.cos(1.57):"+ Math.cos (1.57));//计算双曲余弦
System.out.println ("Math.cosh(1.2 ):"+ Math.cosh (1.2) );//计算正弦
System.out.println ("Math.sin(1.57 ):" + Math.sin (1.57));//计算双曲正弦
System.out.println( "Math.sinh(1.2 ):" + Math.sinh (1.2));//计算三角正切
System.out.println ("Math.tan(0.8):"+ Math.tan(0.8));//计算双曲正切
System.out.println ( "Math.tanh(2.1):" + Math.tanh(2.1));//将矩形坐标(x,y)转换成极坐标(r, thet))
System.out.println ("Math.atan2(0.1,0.2):"+ Math.atan2(0.1,0.2));/*---------下面是取整运算---—-----*/
//取整,返回小于目标数的最大整数
System.out.println ("Math.floor(-1.2 ):" + Math.floor(-1.2 ));//取整,返回大于目标数的最小整数
System.out.println("Math.ceil(1.2):" +Math.ceil (1.2));//四舍五入取整
System.out.println( "Math.round (2.3 ): " + Math.round(2.3 ));/ *---------下面是乘方、开方、指数运算---------*/
//计算平方根
System.out.println ( "Math.sqrt(2.3): " +Math.sqrt(2.3 ));//计算立方根
System.out.println ( "Math.cbrt (9):"+Math.cbrt (9));//返回欧拉数e的n次幂
System.out.println("Math.exp(2):" +Math.exp(2));//返回sqrt(x2 +y2),没有中间溢出或下溢
System.out.println("Math.hypot(4,4):"+Math. hypot (4,4));//按照工EEE 754标准的规定,对两个参数进行余数运算
System.out.println("Math. IEEEremainder(5, 2):"
+ Math.IEEEremainder (5,2));
//计算乘方
System .out.println("Math.pow (3,2):" + Math.pow (3,2));//计算自然对数
System. out.println("Math.log(12): " + Math.log (12));//计算底数为10的对数
System.out.println("Math. log10(9): "+ Math.log10(9));//返回参数与1之和的自然对数
System.out.println ( "Math. log1p(9):" +Math.log1p(9));---------下面是符号相关的运算-------*/
//计算绝对值
System. out.println ("Math.abs (-4.5):"+ Math.abs (-4.5));//符号赋值,返回带有第二个浮点数符号的第一个浮点参数
System.out.println ("Math.copySign (1.2,-1.0):"
+ Math.copysign(1.2,-1.0));
//符号函数,如果参数为0,则返回0;如果参数大于0//则返回1.0;如果参数小于0,则返回-1.0
System.out.println ("Math.signum(2.3): "+ Math.signum(2.3));/*---------下面是大小相关的运算---------*/
//找出最大值
System.out.println ("Math.max(2.3,4.5):" + Math.max(2.3,4.5));//计算最小值
System.out.println ( "Math.min(1.2,3.4):"+ Math.min (1.2,3.4));//返回第一个参数和第二个参数之间与第一个参数相邻的浮点数
System .out.println ( "Math.nextAfter(1.2,1.0):"

random

种子:就是拿来生成随机数的素材,一般取当前时间
电脑产生的是伪随机数

BigDecimal

就是高精度帮你准备好了

import java.math.*;
public class BigDecimal {
    public static void main(String[] args)
    {
        BigDecimal f1 = new BigDecimal("0.05");
        BigDecimal f2 = BigDecimal.valueOf(0.01);
        BigDecimal f3 = new BigDecimal(0.05);
         //使用string作为BigDeciaml参数
        System.out.println("0.05 + 0.01 = " + f1.add(f2));
        System.out.println("0.05 - 0.01 = " + f1.subtract(f2));
        System.out.println("0.05 * 0.01 = " + f1.multiply(f2));
        System.out.println("0.05 / 0.01 = " + f1.divide(f2));
         //使用double作为BigDeciaml参数
        System.out.println("0.05 + 0.01 = " + f3.add(f2));
        System.out.println("0.05 - 0.01 = " + f3.subtract(f2));
        System.out.println("0.05 * 0.01 = " + f3.multiply(f2));
        System.out.println("0.05 / 0.01 = " + f3.divide(f2));
    }
}

用String当参数传进去好一些

Date类 Calender类

var c= Calendar .get Instance();
//取出年
System.out.println (c.get (YEAR));//取出月份
System.out.print1n (c.get (MONTH));//取出日
System.out.println (c.get (DATE));//分别设置年、月、日、小时、分钟、秒
c.set(2003102312,32,23); // 2003-11-2312:32:23
System.out .println(c.getTime());
//将Calendar的年前推1年
c.add (YEAR,-1;//2002-11-23 12:32:23
System.out.println(c.getTime());
//将Calendar的月前推8个月
c.roll (MONTH,-8);//2002-03-23 12:32:23
System.out.println(c.getTime());

java.time

  • Clock:该类用于获取指定时区的当前日期、时间。该类可取代 System类的currentTimeMillis()方法,而且提供了更多方法来获取当前日期、时间。该类提供了大量静态方法来获取 Clock对象。
  • Duration:该类代表持续时间。该类可以非常方便地获取一段时间。
  • Instant:代表一个具体的时刻,可以精确到纳秒。该类提供了静态的 now()方法来获取当前时刻,也提供了静态的 now(Clock clock)方法来获取 clock对应的时刻。除此之外,它还提供了一系列minusXxx()方法在当前时刻基础上减去一段时间,也提供了plusXxx()方法在当前时刻基础上加上一段时间。
  • Instant:代表一个具体的时刻,可以精确到纳秒。该类提供了静态的 now()方法来获取当前时刻,也提供了静态的 now(Clock clock)方法来获取 clock对应的时刻。除此之外,它还提供了一系列minusXxx()方法在当前时刻基础上减去一段时间,也提供了plusXxx()方法在当前时刻基础上加上一段时间。
  • LocalDate:该类代表不带时区的日期,例如 2007-12-03。该类提供了静态的now()方法来获取当前日期,也提供了静态的now(Clock clock)方法来获取 clock对应的日期。除此之外,它还提供了minusXxx()方法在当前年份基础上减去几年、几月、几周或几日等,也提供了plusXxx()方法在当前年份基础上加上几年、几月、几周或几日等。
  • LocalTime:该类代表不带时区的时间,例如 10:15:30。该类提供了静态的 now()方法来获取当前时间,也提供了静态的now(Clock clock)方法来获取 clock对应的时间。除此之外,它还提供了minusXxx()方法在当前年份基础上减去几小时、几分、几秒等,也提供了plusXxx()方法在当前年份基础上加上几小时、几分、几秒等。
  • LocalTime:该类代表不带时区的时间,例如 10:15:30。该类提供了静态的 now()方法来获取当前时间,也提供了静态的now(Clock clock)方法来获取 clock对应的时间。除此之外,它还提供了minusXxx()方法在当前年份基础上减去几小时、几分、几秒等,也提供了plusXxx()方法在当前年份基础上加上几小时、几分、几秒等。
  • MonthDay:该类仅代表月日,例如–04-12。该类提供了静态的 now()方法来获取当前月日,也提供了静态的now(Clock clock)方法来获取clock对应的月日。
  • Year:该类仅代表年,例如2014。该类提供了静态的 now()方法来获取当前年份,也提供了静态的now(Clock clock)方法来获取 clock对应的年份。除此之外,它还提供了minusYears()方法在当前年份基础上减去几年,也提供了plusYears()方法在当前年份基础上加上几年。
  • YearMonth:该类仅代表年月,例如2014-04。该类提供了静态的 now()方法来获取当前年月,也提供了静态的now(Clock clock)方法来获取 clock对应的年月。除此之外,它还提供了minusXxx()方法在当前年月基础上减去几年、几月,也提供了plusXxx()方法在当前年月基础上加上几年、几月。
  • ZonedDateTime:该类代表一个时区化的日期、时间。Zoneld:该类代表一个时区。
  • DayOfWeek:这是一个枚举类,定义了周日到周六的枚举值。Month:这也是一个枚举类,定义了一月到十二月的枚举值。

正则表达式

就是先设计好一个模板,然后一个个去套。
比如手机号的模板是 189-xxxx-xxxx (x代表数字)
那么设计好一个模板(1xx-xxxx-xxxx)就可以在一个超长字符串里面自动慢慢找了。
在这里插入图片描述在这里插入图片描述
还有数量标识符:
在这里插入图片描述

public class FindGroup {
    public class FindGroup
    public static void main (string[] args)
    //使用字符串模拟从网络上得到的网页源码
    var str="我想求购一本《疯狂Java讲义》,尽快联系我13500006666""交朋友,电话号码是13611125565"
        +"出售二手电脑,联系方式15899903312";
    //创建一个 Pattern对象,并用它建立一个 Matcher对象//该正则表达式只抓取13X和15X段的手机号
//实际要抓取哪些电话号码,只要修改正则表达式即可
    Matcher m= Pattern.compile("((13\\d)|(15\d))1Id{8}")
            .matcher (str);
//将所有符合正则表达式的子串(电话号码)全部输出while (m.find())
System.out.println (m.group());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值