您的位置:首页 > 编程语言 > Java开发

重踏学习Java路上_Day14(正则表达式,Math,Random,System,BigInteger,BigDecimal,Date,Calendar)

2015-07-01 09:27 751 查看
1:正则表达式(理解)
(1)就是符合一定规则的字符串,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则。有自己特殊的应用。
(2)常见规则
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,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次
(3)常见功能:(分别用的是谁呢?)
A:判断功能
String类的public boolean matches(String regex)
B:分割功能
String类的public String[] split(String regex)
C:替换功能
String类的public String replaceAll(String regex,String replacement)
D:获取功能
Pattern和Matcher
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");

find():查找存不存在
group():获取刚才查找过的数据
(4)案例
A:判断电话号码和邮箱
B:按照不同的规则分割数据
C:把论坛中的数字替换为*
D:获取字符串中由3个字符组成的单词

/** 分割功能练习*/
public class RegexDemo2 {
public static void main(String[] args) {
// 定义一个字符串
String s1 = "aa,bb,cc";
// 直接分割
String[] str1Array = s1.split(",");
for (int x = 0; x < str1Array.length; x++) {
System.out.println(str1Array[x]);
}
System.out.println("---------------------");

String s2 = "aa.bb.cc";
String[] str2Array = s2.split("\\.");
for (int x = 0; x < str2Array.length; x++) {
System.out.println(str2Array[x]);
}
System.out.println("---------------------");

String s3 = "aa bb cc";
String[] str3Array = s3.split(" +");
for (int x = 0; x < str3Array.length; x++) {
System.out.println(str3Array[x]);
}
System.out.println("---------------------");

//硬盘上的路径,我们应该用\\替代\ 四个\\\\,前两个分别代表java和正则表达式添加,后者表示字符串的两个\\
String s4 = "E:\\JavaSE\\day14\\avi";
String[] str4Array = s4.split("\\\\");
for (int x = 0; x < str4Array.length; x++) {
System.out.println(str4Array[x]);
}
System.out.println("---------------------");
}
}

2:Math(掌握)
(1)针对数学运算进行操作的类
(2)常见方法(自己补齐)

Math:用于数学运算的类。
成员变量:
public static final double //PI
public static final double //E
成员方法:
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)//最大值 (min自学)
public static double pow(double a,double b)//a的b次幂
public static double random()//随机数 [0.0,1.0)
public static int round(float a) //四舍五入(参数为double的自学)
public static double sqrt(double a)//正平方根

(3)案例:
A:猜数字小游戏
B:获取任意范围的随机数

/*
* 需求:请设计一个方法,可以实现获取任意范围内的随机数。
*
* 分析:
* A:键盘录入两个数据。
* int strat;
* int end;
* B:想办法获取在start到end之间的随机数
* 我写一个功能实现这个效果,得到一个随机数。(int)
* C:输出这个随机数
*/
public class MathDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入开始数:");
int start = sc.nextInt();
System.out.println("请输入结束数:");
int end = sc.nextInt();

for (int x = 0; x < 100; x++) {
// 调用功能
int num = getRandom(start, end);
// 输出结果
System.out.println(num);
}
}

/** 写一个功能 两个明确: 返回值类型:int 参数列表:int start,int end*/
public static int getRandom(int start, int end) {
// 回想我们讲过的1-100之间的随机数
// int number = (int) (Math.random() * 100) + 1;
// int number = (int) (Math.random() * end) + start;
// 发现有问题了,怎么办呢?
int number = (int) (Math.random() * (end - start + 1)) + start;
return number;
}
}

3:Random(理解)
(1)用于产生随机数的类
(2)构造方法:
A:Random() 默认种子,每次产生的随机数不同
B:Random(long seed) 指定种子,每次种子相同,随机数就相同
(3)成员方法:
A:int nextInt() 返回int范围内的随机数
B:int nextInt(int n) 返回[0,n)范围内的随机数

/*
* Random:产生随机数的类
*
* 构造方法:
* public Random():没有给种子,用的是默认种子,是当前时间的毫秒值
* public Random(long seed):给出指定的种子
*
* 给定种子后,每次得到的随机数是相同的。
*
* 成员方法:
* public int nextInt():返回的是int范围内的随机数
* public int nextInt(int n):返回的是[0,n)范围的内随机数
*/
public class RandomDemo {
public static void main(String[] args) {
// 创建对象
// Random r = new Random();
Random r = new Random(1111);

for (int x = 0; x < 10; x++) {
// int num = r.nextInt();
int num = r.nextInt(100) + 1;
System.out.println(num);
}
}
}

4:System(掌握)
(1)系统类,提供了一些有用的字段和方法
(2)成员方法

System.gc()可用于垃圾回收。当使用System.gc()回收某个对象所占用的内存之前,通过要求程序调用适当的方法来清理资源。在没有明确指定资源清理的情况下,Java提高了默认机制来清理该对象的资源,就是调用Object类的finalize()方法。finalize()方法的作用是释放一个对象占用的内存空间时,会被JVM调用。而子类重写该方法,就可以清理对象占用的资源,该方法有没有链式调用,所以必须手动实现。 从程序的运行结果可以发现,执行System.gc()前,系统会自动调用finalize()方法清除对象占有的资源,通过super.finalize()方式可以实现从下到上的finalize()方法的调用,即先释放自己的资源,再去释放父类的资源。 但是,不要在程序中频繁的调用垃圾回收,因为每一次执行垃圾回收,jvm都会强制启动垃圾回收器运行,这会耗费更多的系统资源,会与正常的Java程序运行争抢资源,只有在执行大量的对象的释放,才调用垃圾回收最好

System类包含一些有用的类字段和方法。它不能被实例化。

A.public static void gc():运行垃圾回收器。执行gc方法前会调用finalize方法
强制立即回收垃圾,即释放内存。
java对内存的释放采取的垃圾自动回收机制,在编程的时候,java虚拟机可以自动判断出并收集的垃圾,但一般不会立即释放它们的内存空间,当然也可以通过调用 System.gc()方法,来强制垃圾回收,但是要注意的是,系统并不保证会立即进行释放内存,不确定释放时间,或者什么释放干活。
public class SystemDemo {
public static void main(String[] args) {
Person p = new Person("赵雅芝", 60);
System.out.println(p);

p = null; // 让p不再指定堆内存
System.gc();
}
}
public class Person {
private String name;
private int age;
public Person() {
super();
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
@Override
protected void finalize() throws Throwable {
System.out.println("当前的对象被回收了" + this);
super.finalize();
}
}

结果输出:
Person [name=周杰伦, age=28]
你调用了Person的Finalize()方法

B:退出jvm
System.out.println("我们喜欢林青霞(东方不败)");
System.exit(0);
System.out.println("我们也喜欢赵雅芝(白娘子)");

C:获取当前时间的毫秒值
public static long currentTimeMillis():返回以毫秒为单位的当前时间
public class SystemDemo {
public static void main(String[] args) {
// System.out.println(System.currentTimeMillis());

// 单独得到这样的实际目前对我们来说意义不大
// 那么,它到底有什么作用呢?
// 要求:请大家给我统计这段程序的运行时间
long start = System.currentTimeMillis();
for (int x = 0; x < 100000; x++) {
System.out.println("hello" + x);
}
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
}
}

D:数组复制
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。
public class SystemDemo {
public static void main(String[] args) {
// 定义数组
int[] arr = { 11, 22, 33, 44, 55 };
int[] arr2 = { 6, 7, 8, 9, 10 };

// 请大家看这个代码的意思
System.arraycopy(arr, 1, arr2, 2, 2);

System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
}
}
输出结果:
[11, 22, 33, 44, 55]
[6, 7, 22, 33, 10]

5:BigInteger(理解)
(1)针对大整数的运算 可以让超过Integer范围内的数据进行运算

public static void main(String[] args) {
// 这几个测试,是为了简单超过int范围内,Integer就不能再表示,所以就更谈不上计算了。
// Integer i = new Integer(100);
// System.out.println(i);
// // System.out.println(Integer.MAX_VALUE);
// Integer ii = new Integer("2147483647");
// System.out.println(ii);
// // NumberFormatException
// Integer iii = new Integer("2147483648");
// System.out.println(iii);

// 通过大整数来创建对象
BigInteger bi = new BigInteger("2147483648");
System.out.println("bi:" + bi);
}

(2)构造方法
A:BigInteger(String s)
(3)成员方法

/*
* public BigInteger add(BigInteger val):加
* public BigInteger subtract(BigInteger val):减
* public BigInteger multiply(BigInteger val):乘
* public BigInteger divide(BigInteger val):除
* public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组
*/
public class BigIntegerDemo {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("100");
BigInteger bi2 = new BigInteger("50");

// public BigInteger add(BigInteger val):加
System.out.println("add:" + bi1.add(bi2));
// public BigInteger subtract(BigInteger val):减
System.out.println("subtract:" + bi1.subtract(bi2));

// public BigInteger multiply(BigInteger val):乘
System.out.println("multiply:" + bi1.multiply(bi2));

// public BigInteger divide(BigInteger val):除
System.out.println("divide:" + bi1.divide(bi2));

// public BigInteger[] divideAndRemainder(BigInteger val):返回商和余数的数组
BigInteger[] bis = bi1.divideAndRemainder(bi2);
System.out.println("商:" + bis[0]);
System.out.println("余数:" + bis[1]);
}
}

6:BigDecimal(理解)
(1)浮点数据做运算,会丢失精度。所以,针对浮点数据的操作建议采用BigDecimal。(金融相关的项目) 丢失精度原因,由于浮点数保存在小数部分采取乘2,余数再乘2,直至为0,这样遇上无限不循环小数,保存会丢失,而且读取出来也会丢失,所以精度会损失。

由于在运算的时候,float类型和double很容易丢失精度,演示案例。所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal

BigDecimal类概述

不可变的、任意精度的有符号十进制数。

//出现问题的例子
public static void main(String[] args) {
System.out.println(0.09 + 0.01);
System.out.println(1.0 - 0.32);
System.out.println(1.015 * 100);
System.out.println(1.301 / 100);

System.out.println(1.0 - 0.12);
}

/*
* 看程序写结果:结果和我们想的有一点点不一样,这是因为float类型的数据存储和整数不一样导致的。它们大部分的时候,都是带有有效数字位。
*
* 由于在运算的时候,float类型和double很容易丢失精度,演示案例。所以,为了能精确的表示、计算浮点数,Java提供了BigDecimal
*
* BigDecimal类:不可变的、任意精度的有符号十进制数,可以解决数据丢失问题。
*/

输出结果:
0.09999999999999999
0.6799999999999999
101.49999999999999
0.013009999999999999
0.88

(2)构造方法
A:BigDecimal(String s)
(3)成员方法:

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): //除法,其他看api ,商,几位小数,如何舍取

public class BigDecimalDemo {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("0.09");
BigDecimal bd2 = new BigDecimal("0.01");
System.out.println("add:" + bd1.add(bd2));
System.out.println("-------------------");

BigDecimal bd3 = new BigDecimal("1.0");
BigDecimal bd4 = new BigDecimal("0.32");
System.out.println("subtract:" + bd3.subtract(bd4));
System.out.println("-------------------");

BigDecimal bd5 = new BigDecimal("1.015");
BigDecimal bd6 = new BigDecimal("100");
System.out.println("multiply:" + bd5.multiply(bd6));
System.out.println("-------------------");

BigDecimal bd7 = new BigDecimal("1.301");
BigDecimal bd8 = new BigDecimal("100");
System.out.println("divide:" + bd7.divide(bd8));
System.out.println("divide:"+ bd7.divide(bd8, 3, BigDecimal.ROUND_HALF_UP));
System.out.println("divide:"+ bd7.divide(bd8, 8, BigDecimal.ROUND_HALF_UP));
}
}

输出结果:
add:0.10
-------------------
subtract:0.68
-------------------
multiply:101.500
-------------------
divide:0.01301
divide:0.013
divide:0.01301000

7:Date/DateFormat(掌握)
(1)Date是日期类,可以精确到毫秒。
A:构造方法

/*
* Date:表示特定的瞬间,精确到毫秒。
*
* 构造方法:
* Date():根据当前的默认毫秒值创建日期对象
* Date(long date):根据给定的毫秒值创建日期对象
*/
public class DateDemo {
public static void main(String[] args) {
// 创建对象
Date d = new Date();
System.out.println("d:" + d);

// 创建对象
// long time = System.currentTimeMillis();
long time = 1000 * 60 * 60; // 1小时
Date d2 = new Date(time);
System.out.println("d2:" + d2); 这里会输出:d2:Thu Jan 01 09:00:00 CST 1970 ,按常理来说应该是1点的,但是这里是东八区,所以要加8,所以为9点。
}
}

B:成员方法

/*
* public long getTime():获取时间,以毫秒为单位
* public void setTime(long time):设置时间
*
* 从Date得到一个毫秒值
* getTime()
* 把一个毫秒值转换为Date
* 构造方法
* setTime(long time)
*/
public class DateDemo {
public static void main(String[] args) {
// 创建对象
Date d = new Date();

// 获取时间
long time = d.getTime();
System.out.println(time);
// System.out.println(System.currentTimeMillis());

System.out.println("d:" + d);
// 设置时间
d.setTime(1000);
System.out.println("d:" + d);
}
}

C:日期和毫秒值的相互转换
案例:你来到这个世界多少天了?
(2)DateFormat针对日期进行格式化和针对字符串进行解析的类,但是是抽象类,所以使用其子类SimpleDateFormat
A:SimpleDateFormat(String pattern) 给定模式
yyyy-MM-dd HH:mm:ss
B:日期和字符串的转换
a:Date -- String
format()

b:String -- Date
parse()
C:案例:
制作了一个针对日期操作的工具类。



/*
* 算一下你来到这个世界多少天?
*
* 分析:
* A:键盘录入你的出生的年月日
* B:把该字符串转换为一个日期
* C:通过该日期得到一个毫秒值
* D:获取当前时间的毫秒值
* E:用D-C得到一个毫秒值
* F:把E的毫秒值转换为年
* /1000/60/60/24
*/
public class MyYearOldDemo {
public static void main(String[] args) throws ParseException {
// 键盘录入你的出生的年月日
Scanner sc = new Scanner(System.in);
System.out.println("请输入你的出生年月日:");
String line = sc.nextLine();

// 把该字符串转换为一个日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date d = sdf.parse(line);

// 通过该日期得到一个毫秒值
long myTime = d.getTime();

// 获取当前时间的毫秒值
long nowTime = System.currentTimeMillis();

// 用D-C得到一个毫秒值
long time = nowTime - myTime;

// 把E的毫秒值转换为年
long day = time / 1000 / 60 / 60 / 24;

System.out.println("你来到这个世界:" + day + "天");
}
}

8:Calendar(掌握)
(1)日历类,封装了所有的日历字段值,通过统一的方法根据传入不同的日历字段可以获取值。
(2)如何得到一个日历对象呢?
Calendar rightNow = Calendar.getInstance();
本质返回的是子类对象

API:

Calendar
提供了一个类方法
getInstance
,以获得此类型的一个通用的对象。
Calendar
getInstance
方法返回一个
Calendar
对象,其日历字段已由当前日期和时间初始化:


Calendar rightNow = Calendar.getInstance();



(3)成员方法
A:根据日历字段得到对应的值
B:根据日历字段和一个正负数确定是添加还是减去对应日历字段的值
C:设置日历对象的年月日

/*
* public void add(int field,int amount):根据给定的日历字段和对应的时间,来对当前的日历进行操作。
* public final void set(int year,int month,int date):设置当前日历的年月日
*/
public class CalendarDemo {
public static void main(String[] args) {
// 获取当前的日历时间
Calendar c = Calendar.getInstance();

// 获取年
int year = c.get(Calendar.YEAR);
// 获取月
int month = c.get(Calendar.MONTH);
// 获取日
int date = c.get(Calendar.DATE);
System.out.println(year + "年" + (month + 1) + "月" + date + "日");

// // 三年前的今天
// c.add(Calendar.YEAR, -3);
// // 获取年
// year = c.get(Calendar.YEAR);
// // 获取月
// month = c.get(Calendar.MONTH);
// // 获取日
// date = c.get(Calendar.DATE);
// System.out.println(year + "年" + (month + 1) + "月" + date + "日");

// 5年后的10天前
c.add(Calendar.YEAR, 5);
c.add(Calendar.DATE, -10);
// 获取年
year = c.get(Calendar.YEAR);
// 获取月
month = c.get(Calendar.MONTH);
// 获取日
date = c.get(Calendar.DATE);
System.out.println(year + "年" + (month + 1) + "月" + date + "日");
System.out.println("--------------");

c.set(2011, 11, 11);
// 获取年
year = c.get(Calendar.YEAR);
// 获取月
month = c.get(Calendar.MONTH);
// 获取日
date = c.get(Calendar.DATE);
System.out.println(year + "年" + (month + 1) + "月" + date + "日");
}
}

(4)案例:
计算任意一年的2月份有多少天?

/*
* 获取任意一年的二月有多少天
*
* 分析:
* A:键盘录入任意的年份
* B:设置日历对象的年月日
* 年就是A输入的数据
* 月是2
* 日是1
* C:把时间往前推一天,就是2月的最后一天
* D:获取这一天输出即可
*/
public class CalendarTest {
public static void main(String[] args) {
// 键盘录入任意的年份
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt();

// 设置日历对象的年月日
Calendar c = Calendar.getInstance();
c.set(year, 2, 1); // 其实是这一年的3月1日
// 把时间往前推一天,就是2月的最后一天
c.add(Calendar.DATE, -1);

// 获取这一天输出即可
System.out.println(c.get(Calendar.DATE));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: