复习:
1. String:最终类,常量,以字符数组形式来存储数据 --- 操作不改变原字符串
Pattern(续接前一天)
指定规则进行范围性/模糊匹配
在正则中用()将某一块作为一个整体来操作 --- 捕获组
Java中的正则会对其中的捕获组进行编号,从1开始 --- 从‘(’ 开始计算
//如果报错请自行检查里面的命名是否有重复,因为是几个Demo合成在一起了
public static void main(String[] args) {
String str2="12sd31a45sd4asd1as35";
System.out.println(str2.replace("1", "-"));
//将所有数字替换为-
System.out.println(str2.replaceAll("\\d", "-"));
//将所有非数字全部去掉
System.out.println(str2.replaceAll("\\D", ""));
//替换掉第一次匹配到的内容
System.out.println(str2.replaceFirst("\\d", "-"));
String str="asc acs cas asc";
//交换acs cas
//在replaceAll中可以$n的形式引用前一个正则中对应编号的捕获组
System.out.println(str.replaceAll("(asc )(acs )(cas )(asc)", "$1$3$2$4"));
//叠字清除
String str1="开开心心学学习习";
System.out.println(str1.replaceAll("(.)\\1+", "$1"));
String str="1asd84as1d3a1da4d1as1da54sd4588";
//以数字作为切割符将字符串切开
//作为切割符的符号会被切掉
//在字符串最末尾的切割符会被整个切掉
//除了在末尾的切割符以外,相邻的两个切割符之间会切出一个空字符串
String[] strs=str.split("\\d");
System.out.println(Arrays.toString(strs));
}
练习:
1. 邮箱格式的校验:
[email protected] [email protected]
[email protected] [email protected]
//这里是两种验证,第一种是比较全面的
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
String regex="[a-zA-Z0-9_]+@[a-zA-Z0-9_]+\\.(((com\\.)?cn)|com)";
System.out.println(str.matches(regex));
System.out.println(str.matches("[a-zA-Z0-9]{6,32}@[a-zA-Z0-9]+(\\.com)")
|| str.matches("[a-zA-Z0-9]{6,32}@[a-zA-Z0-9]+(\\.com)?(\\.cn)"));
}
2. 输入一个字符串,统计其中每一个字符出现的次数
public static void main(String[] args) {
//获取字符串
Scanner sc=new Scanner(System.in);
String str=sc.nextLine();
while(str.length()>0){
//取得首字符
char cs = str.charAt(0);
//获取原长度
int len=str.length();
//替换与首字符相同的字符
str=str.replaceAll(String.valueOf(cs), "");
//字符个数就是字符减少的个数
System.out.println(cs+":"+(len-str.length()));
}
}
3. 对于任意的字符串可以认为是由多组叠字或者是单字来拼成的,这每一组叠字/单字都是字符串的碎片,计算一个字符串平均的碎片长度
aaabbbbbcddaaacc -> aaa bbbbb c dd aaa cc -> (3 + 5 + 1 + 2 + 3 + 2) / 6 = 2.67
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
// 记录字符串的原长度 --- 碎片的总长度
double len = str.length();
// 将字符串中所有的叠字替换为单字
str = str.replaceAll("(.)\\1+", "$1");
// 替换之后的长度就是碎片的个数
// 计算碎片的平均长度
System.out.println(len / str.length());
}
包装类
基本类型身上没有属性和方法让便捷的操作数据,因此针对每一种基本类型提供了对应的类形式 --- 包装类
byte |
short |
int |
long |
float |
double |
char |
boolean |
void |
Byte |
Short |
Integer |
Long |
Float |
Double |
Character |
Boolean |
Void |
Void是最终类,不能被实例化
当把基本类型的变量直接赋值给对应的引用类型的对象 --- 自动封箱,底层是调用了对应类身上的valueOf方法
当把引用类型的对象直接赋值给对应的基本类型的变量 --- 自动拆箱,底层是调用了对应对象身上的**Value方法
四种整数型都有范围判断(-128~127)
注意:凡是Number的子类都会提供将字符串转化为对应类型的构造方法
包装类产生的对象的哈希码是固定的,不随环境改变
注意:字面量的哈希码是不随环境不随系统而改变
public static void main(String[] args) {
System.out.println(new Integer(5).hashCode());
System.out.println(new Long(5).hashCode());
System.out.println(new Character('a').hashCode());
System.out.println(new Boolean(true).hashCode());
System.out.println(new Boolean(false).hashCode());
// Integer i=10;
// System.out.println(Math.E);
// System.out.println(Math.PI);
int i = 10;
// 利用基本类型的数据构建了对应的引用类型的对象 ---封箱
Integer in = new Integer(i);
// 自动封箱/装箱是JDk1.5的特性之一
// 将基本类型的数据直接复制给了对应的引用类型的对象 ---自动封箱
// 自动封箱在底层会默认调用Integer类身上的静态方法valueOf
// Integer in1=Integer.valueOf(i);
Integer in1 = i;
double d = 3.4;
Double dou = Double.valueOf(d);
Double dou1 = d;
char c = 'a';
Character ch = c;
Integer in2 = new Integer(i);
// 将引用类型的对象直接复制给了对应的基本类型的变量 --- 自动拆箱
// 自动拆箱在底层会默认调用对象上的***Value()
// int i1=in.intValue();
int i1=in2;
Double dou2=new Double(2.8);
Double d1=dou2.doubleValue();
double d2=dou2;
Boolean bo=new Boolean(false);
boolean b=bo.booleanValue();
boolean b1=bo;
//Integer i1=Integer.valueOf
//如果值不在-128~127范围内
//底层的valueOf方法是调用了构造方法来返回一个新的对象
//如果值在-128~127范围内
//从cache数组对应的下标位置上取值
Integer i1=-125;
Integer i2=-125;
Integer i3=-129;
Integer i4=-129;
System.out.println(i1==i2);
System.out.println(i3==i4);
Integer in=400;
int i=400;
//当包装类与基本类型在运算时会进行自动拆箱
System.out.println(in==i);
Integer in = new Integer("-284");
System.out.println(in);
int i = Integer.parseInt("32");
System.out.println(i);
System.out.println(new Double("3.256"));
// 不考虑大小写的情况下不是true就是false
System.out.println(new Boolean("true"));
// NaN是唯一的一个值
// NaN不与任何东西相等,包括自己本身
System.out.println(Double.NaN == Double.NaN);
System.out.println(Double.isNaN(3.3 / 1.32));
}
数学类
Math --- 最终类,针对基本类型提供了基本的数学运算
public static void main(String[] args) {
//获取绝对值
System.out.println(Math.abs(-20));
//求立方根
System.out.println(Math.cbrt(8));
//向上取整
System.out.println(Math.ceil(2.3));
//向下取整
System.out.println(Math.floor(2.3));
//四舍五入
System.out.println((double)Math.round(2.568));
//四舍五入并保留两位小数
System.out.println((double)Math.round(3.625*100)/100);
//返回一个[0,1)的随机小数
System.out.println(Math.random());
//取45~90之间的随机整数
System.out.println(Math.random()*45+45);
//获取0~30随机整数
Random r=new Random();
System.out.println(r.nextInt(30));
//中奖几率0.15%
for(int i=0;i<10000;i++){
if(Math.random()*Math.random()>0.95)
System.out.println("dele");
}
}
BigDecimal --- 能够精确运算小数,要求参数以字符串形式传递
//原来在程序中事宜64位二进制来计算小数
//strictfp 要求方法在计算过程中以80位二进制来进行存储
//但是计算完成后仍然要求结果以64位二进制进行存储
public strictfp static void main(String[] args){
//十进制小数化为二进制的时候本身就不精确
double d1=3.26;
double d2=3.62;
System.out.println(d2-d1);
//如果想要精确运算,需要将参数以字符串形式传递
BigDecimal bd1=new BigDecimal(3.26);
BigDecimal bd2=new BigDecimal(3.62);
BigDecimal bd3=new BigDecimal("3.26");
BigDecimal bd4=new BigDecimal("3.62");
System.out.println(bd2.subtract(bd1));
System.out.println(bd4.subtract(bd3));
}
DecimalFormat --- 进行数字的格式化的类
public static void main(String[] args) {
double price = 10.00;
double real = price * 0.95;
// 0表示一位数字,如果这一位没有计算出数字,那么会默认以0作为填充
// #表示这一位如果有数字则填充,如果没有则不填充
DecimalFormat df = new DecimalFormat("#0.00");
// DecimalFormat df=new DecimalFormat("00.00");
System.out.println(df.format(real));
long l = 456687656546L;
// 将这个值转化为科学计数法形式
DecimalFormat df1 = new DecimalFormat("0.##E0");
// 设置近似值
df1.setRoundingMode(RoundingMode.CEILING);
String str = df1.format(l);
System.out.println(str);
}
BigInteger --- 能够计算任意大的整数,要求参数以字符串形式传入
public static void main(String[] args) {
//计算任意大的数(6千多万位)
//需要将过大的整数以字符串形式传入
BigInteger b1=new BigInteger("132135465");
BigInteger b2=new BigInteger("132135465");
System.out.println(b1.multiply(b2));
}
日期类
Date --- 重点掌握字符串和日期之间的转换 --- SimpleDateFormat
Calendar --- 抽象类
public static void main(String[] args) throws Exception {
//获取当前系统时间
Date date=new Date();
System.out.println(date);
//这个方法在1900-01的基础上向上累加
//@SuppressWarnings("deprecation")
//这个方法已过时
//已过时是指可以使用但是不推荐使用,在后续版本或许会随时抛弃
Date date1=new Date(2000 - 1900, 2 - 1, 9);
//2000-12-15
//2000.12.15
//2000/12/15
//12/15/2000
//90.12.15
//将字符串转化为日期
//parse:将字符串转化为日期 , format:将日期转化为字符串
SimpleDateFormat sdf=new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date date3=sdf.parse("2000.12.15 18:58:45");
System.out.println(sdf.format(new Date()));
//将日期转化为字符串
//XX年XX月XX日
//XX时XX分XX秒
SimpleDateFormat sdf2=new SimpleDateFormat("yy年MM月dd日\r\nHH时mm分ss秒");
System.out.println(sdf2.format(new Date()));
Calendar c=Calendar.getInstance();
System.out.println(c);
}