Java第八章——常用类

一、String 类

(1)存在于Java.lang包中,用于处理字符串。
①对于常量字符串,如“你好”“FUC”型的,会被放在常量池,变量定义的字符串会被放进动态区
②常量池的引用会指向已有的内存,如果不存在再创建一个地区给新定义的常量。但动态区的变量每次都要新创建
(2)String类常用的方法
①public int length()
很明显,是测量字符串长度的
②pubic boolean equal() 比较两个字符串是否一样
String a=“disc”
String b=“dc”
a.equal(b)这样用
③public int compareTo(String a)
比大小

二、正则表达式

(1)概念:正则表达式是一个String的字符串序列,这序列含有特殊意义的字符,可以用作匹配使用

在表达式中的写法       意义

\\d                 0-9中的任何一个数字
\\D                 任何一个非数字字符               
\\p{Lower}          小写字母            
\\p{Upper}          大写字母
\\p{Alpha}             字母
\\p{Punct}           标点符号
\\p{Blank}          空格或者制表符【\t】

例如:String regex="\d"代表的是0-9中的任何一个数字
现在有String a=“0” ,那么 a.matches(regex)就是true

(2)限定修饰符
定义:在匹配的时候控制匹配条件用的

模式                意义

X?                出现0次或1次
X*                出现0次或多次
X+                出现1次或多次
X{n}                出现n次
X{n,}                出现大于等于n次
X{n,m}                出现n-m次(包含)
XY                后缀为Y

例如

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String res="\\d+";//字符串在0-9出现大于等于1次。。。。其它的限定符用法一样
		String a="0";          //0出现在\\d中一次,所以会输出在
		if(a.matches(res)){
			System.out.println("在");
		}
		else
			System.out.println("不在");
	}
}

(3)类似于限定符的还有括号的嵌套

    [a-d[123]]    称之为“并”,代表a-d和123
    [a-d&&[a-c]]  称之为“交”,代表[a-d]和[a-c]共有的部分
    [a-d&&[^bc]]  称之为差

(4)split()

一个用于分解字符串的函数,配合正则表达式使用效果很好,用法看代码

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("输入一句英语:");
		String res="[\\d\\p{Punct}\\s]+";                         //分解的标准
		Scanner scanner=new Scanner(System.in);
		String a=scanner.nextLine();
		String []words=a.split(res);
		for(int i=0;i<a.length();i++){
			System.out.println(words[i]);
		}
		
	}

在这里插入图片描述

(5)Date与Calendar类

①Date的用法

Date date=new Date();得出本机系统的时间

Date date =new Date(1000) //计算机系统的默认时间是设置在1970年1月1日,使用这个方法new 出来的时间是在默认时间的多少毫秒以后

②Calendar类
老规矩还是先创建对象
方法:Calendar canlendar=new getInstance()
设置年月日的方法:

calendar.set(int year,int month,int date.....)

另外一个比较有用的方法是getTimeInMillis(),可以获得对象时间与格林威治时间的差。

	public static void main(String[] args) {
		// TODO Auto-generated method stub.
		Calendar calendar1=Calendar.getInstance();  //getInstance()一个日历对象
		calendar1.set(2016, 1,7);        //设置time1的时间是2016年1月7日
		long time1=calendar1.getTimeInMillis();      //现在时间与格林威治时间的差
		Calendar calendar2=Calendar.getInstance();
		calendar2.set(2019, 1,7);
		long time2=calendar2.getTimeInMillis();          
		System.out.println((time2-time1)/1000/60/60/24/365);

	}

下面是一个输出某年某月的日历,要求用户输入哪年哪月,然后界面输出这月的日历。
解题思路:虽然代码不长,但是有几点是让我想不通的:
(1)当用户输入1的时候表示他想输出1月的日历,而在calendar.set(int year,int month,int day)这个方法里,month等于0相当于是我们现实世界里的1月,所以设置的时候要减1
(2)某月的1号不一定是周一,那么1号的前面一定是空的,所以在1号之前就输出【“”】空。这月的最后一天之后也是有空的,采取同样的方法,具体做法见下面的代码。
(3)输出时候的问题最难解决,因为要输出我们平时见到的那样的日历格式,而在calendar.get(Calendar.DAY_OF_WEEK)这个方法里,0代表周日,1代表周一,2代表周二。。。。
在书上输出的格式是“日一 二三四五六”,这位大神是让calendar.get(Calendar.DAY_OF_WEEK)取得的值减少1,那样正好符合上面的输出格式(因为0就是周日啊)。而我电脑上的输出格式是“一二三四五六日”,那我怎么才能输出这样的格式呢?观察一下就是把一相对于DAY_OF_WEEK得出的数字向左移动两位,所以我是减去2.刚好形式上跟我的电脑输出的一样。
PS:另外我本来是想不看书上的代码自己做的,思考了半小时思路写的越来越乱,看了书上的代码,才反应到大神的解题思路让我敬佩,代码简直写的美感,666.

package 日历;

import java.util.Calendar;

public class CalendarBean {
	int year;
	int month;
	public void SetYear(int year){
		this.year=year;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public String [] getCalendar(){
		String []a=new String [42]; //一个42位的数组,因为如果恰逢1号是周六,
		                            //又是30或者31天的月数就需要大于35的长度
		int day;   //这个月的天数
		Calendar calendar=Calendar.getInstance();
		calendar.set(year,month-1,1);  //这里月份要减1,因为0代表1月
		int weekDay=calendar.get(Calendar.DAY_OF_WEEK)-2;    //算出这月的1号是周几
		if(month==1||month==3||month==5||month==7||month==10||month==12){
			day=31;
		}
		else if(month==4||month==6||month==8||month==9||month==11){
			day=30;
		}
		else{
			if((year%4==0)&&(year%100!=0)||(year%400)==0){
				day=29;
			}
			else{
				day=28;
			}
		}
		for(int i=0;i<weekDay;i++){        //一号之前的位置都是空
			a[i]="";
		}
		for(int i=weekDay,n=1;i<weekDay+day;i++){
			a[i]=String.valueOf(n);                     //int型转为String
			n++;
		}
		for(int i=weekDay+day;i<42;i++){          //Day后面的也都是空
			a[i]="";
		}
		return a;
		
	}
}

package 日历;

import java.util.Scanner;

public class 主函数 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("请输入要输出的年月:");
		Scanner reader=new Scanner(System.in);
		int year=reader.nextInt();
		int month=reader.nextInt();
		CalendarBean calendarBean=new CalendarBean();
		calendarBean.SetYear(year);
		calendarBean.setMonth(month);
		String []a=calendarBean.getCalendar();        //调用计算方法
		char []Wek={'一','二','三','四','五','六','日'};
		for(char wek:Wek){   //一种遍历的写法,其中wek代表遍历出来的变量
			System.out.printf("%4c",wek);
		}
		System.out.println("");
		for(int i=0;i<a.length;i++){
			if(i%7==0){                    //7个换一次行
				System.out.println("");
			}
			System.out.printf("%3s",a[i]);
		}

	}

}

在这里插入图片描述

(6)Math常用方法

package Math类;

public class test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a1=1;
		int a2=-1;
		int a3=Math.abs(a2);       //求绝对值
		System.out.println("abs:"+a3);
		int a4=Math.max(a1, a2);     //求最大值
		System.out.println("max:"+a4);
		double a5=-12.501;
		double a6=Math.round(a5);     //round四舍五入的函数,对于正数四舍五入,对于负数先取正再四舍五入,再添加负号
		System.out.println("round:"+a6);//应该是-13
		double a7=-12.50;
		double a8=Math.round(a7);
		System.out.println("round2"+a8);//应该是-12
		double a9=Math.random();//产生应该0~1的随机数(包括0,不包括1)
		System.out.println("随机数:"+a9);
		int a10=4;
		double a11=Math.sqrt(a10);
		System.out.println("平方根:"+a11);
		
		System.out.println("E常量:"+Math.E+"     Π常量:"+Math.PI);//Math有两个常量E和PI
		
		
	}

}


在这里插入图片描述

(7)BigInteger类

顾名思义,一个定义超大整数的类

package Math类;

import java.math.BigInteger;

public class Big大数 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BigInteger bigInteger=new BigInteger("123456789"); //括号里的数字是String类型的常量,所以要用双引号,
		                                                   //刚刚我就错了。。。
		BigInteger bigInteger2=new BigInteger("12884173381");
		BigInteger bigInteger3= bigInteger.multiply(bigInteger2);  //两个超大数求积
		System.out.println("两个数的积是:"+bigInteger3);

	}

}

在这里插入图片描述

(8)Random类

一个可以定义变量random的类,而random变量一个方法nextInt(int max)可以自定义生成[0,max)之间的整数。

package Math类;

import java.util.Random;

public class 随机数 {
	public static int [] GetRandom(int max,int count ){
		int[]RandomNumber=new int[count];
		int index=0;
		Random random=new Random();
		while(index<count){        
			int num=random.nextInt(max);   //生成0~max之间的一个随机整数[0,max)
			RandomNumber[index]=num;         //把这个数存进数组
			index++;
		}
		return RandomNumber;
		
	}
}

package Math类;

public class 随机数主函数 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		随机数 suiji=new 随机数();
		int []a=suiji.GetRandom(10, 6);
		System.out.println(java.util.Arrays.toString(a));

	}

}

在这里插入图片描述
(9)Pattern类的Match类
两个互相配合的类,用于匹配字符串

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String st="dsncnjcksjHellosdsdjk";  //被匹配的字符串
		String rex="Hello";
		Pattern pattern=Pattern.compile(rex); //声明一个Pattern类型变量去寻找rex的匹配串
		Matcher m=pattern.matcher(st);    //实现方法
		if(m.find()){         //是否找到
			System.out.println("find");}
		

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值