(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]
\D 非數字:[^0-9]
\w 單詞字符:[a-zA-Z_0-9]
在正則表達式裏面組成單詞的東西必須有這些東西組成
D:邊界匹配器
^ 行的開頭
$ 行的結尾
\b 單詞邊界
就是不是單詞字符的地方
舉例:hello78?asdf;ads ? 和; 是單詞邊界
E:Greedy 數量詞
X? X,一次或一次也沒有
X* X, 零次或多次
X+ X,一次或多次
X{n} X,恰好n次
X{n,} X,至少n次
X{n,m} X,至少n次,但是不超過m次
2 正則表達式的判斷功能
String 類的public boolean matches(String regex)
分析:
A:鍵盤錄入手機號碼
B:定義手機號碼的規則
13436799954
13123456789
18123456789
18987564231
C:調用功能,判斷即可
D:輸出結果
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你的手機號碼:");
String phone = sc.nextLine();
//定義手機號碼的規則
String regex = "1[38]\\d{9}";//1開頭然後是3或者8 然後0-9 9次
//調用功能,判斷即可
boolean flag = phone.matches(regex);
例子:郵箱校驗案例
[email protected]
[email protected]
[email protected]
[email protected]
//開頭任意字母或數組 次數1次以上(+) 然後是@ 然後任意字母或數字 次數2-6 然後. 然後任意字母或數字 次數2-3次
String regex = "[a-zA-Z_0-9]+@[a-zA-Z_0-9]{2,6}\\.[a-zA-Z_0-9]{2,3}"
String類的public String[] split(String regex)
根據給定正則表達式的匹配拆分此字符串
舉例:
百合網,世紀佳緣,珍愛網
搜索好友:
性別:女
範圍:"18-24"
age>=18 && age<=24
//定義一個年齡搜索範圍
String ages = "18-24";
//定義規則
String regex = "-";
//調用方法
String[] strArray = ages.split(regex);
//遍歷
for(int x=0;x<strArray.length;x++){
System.out.println(strArray[x]);
}
//輸出 18 24
//如何得到int類型的呢?
int startAge = Integer.parseInt(strArray[0]);
int endAge = Integer.parseInt(strArray[1]);
//鍵盤錄入年齡
Scanner sc = new Scanner(System.in);
System.out.println("請輸入你的年齡:");
int age = sc.nextInt();
if(age>=startAge && age <=endAge){
System.out.println("符合要求");
}else{
System.out.println("不符合要求");
}
// 輸入30 不符合要求 輸入20 符合要求
-----------------------
分割功能練習
A----------------------
//定義一個字符串
String s1 = "aa,bb,cc";
//直接分割
String[] str1Array = s1.split(",");
for(int x = 0;x<str1Array.length;x++){
System.out.println(str1Array[x]);
}
//輸出 aa
bb
cc
B-----------------------
String s2 = "aa.bb.cc";
String[] str2Array = s2.split("\\.");
for(int x = 0;x<str2Array.length;x++){
System.out.println(str2Array[x]);
}
//輸出 aa
bb
cc
C------------------------
String s3 = "aa bb cc";
String[] str3Array = s3.split(" +"); //空格+
for(int x = 0;x<str3Array.length;x++){
System.out.println(str3Array[x]);
}
//輸出 aa
bb
cc
**D------------------------
// 硬盤上的路徑,我們應該用\\替代
String s4 = "E:\\JavaSE\\day14\\avi";
String[] str4Array = s4.split("\\\\"); //\\\\代表\\
for(int x = 0;x<str4Array.length;x++){
System.out.println(str4Array[x]);
}
4 正則表達式的替換功能
String類的public String repalceAll(String regex,String replacement)
使用給定的replacement 替換此字符串所有匹配給定的正則表達式的子字符串
//定義一個字符串
String s = "hello12345world6221123456789java";
//我要去除所有的數字,用*給替換掉
String regex = "\\d+";
String ss = "*"; //給一個* 當把+去掉時,有多少的數字給多少的*
String result = s.replaceAll(regex,ss);
//直接去掉數字,不給*
String regex = "\\d+";
String ss = "";
String result = s.replaceAll(regex,ss);
5 獲取功能
Pattern 和Matcher類的使用
模式和匹配器的基本使用順序
//模式和匹配器的典型調用順序
//把正則表達式編譯成模式對象
Pattern p = Pattern.compile("a*b");
//通過模式對象得到匹配器對象,這個時候需要的是被匹配的字符串
Matcher m = p.matcher("aaaab");
//調用匹配器對象的功能
boolean b = m.matches();
System.out.println(b);
//輸出:true
//這個是判斷功能,但是如果做判斷,這樣就有點麻煩,我們直接用字符串的方法做
String s = "aaaab";
String regex = "a*b";
boolean bb = s.matches(regex);
System.out.println(bb);
//輸出:true
----------------------
獲取功能: 獲取下面這個字符串中由三個字符串組成的單詞
da jia ting shuo guo mei ,jin tian shi ge hao tian qi,ke yi chu qu wan l 。
//定義字符串
String s =" da jia ting shuo guo mei ,jin tian shi ge hao tian qi,ke yi chu qu wan l 。";
//規則
String regex ="\\w{3}\\b"; //\\b 表示沒有邊界
//把規則編譯成模式對象
Pattern p = Pattern.compile(regex);
//通過模式對象得到匹配器對象
Matcher m = p.matcher(s);
------------------------------------
//調用匹配器對象的功能
//通過find方法 就是查找有沒有滿足條件的子串
// public boolean find()
boolean flag = m.find();
System.out.println(flag);
//如何得到值呢?
//public String group()
String ss = m.group();
System.out.println(ss);
// 再來一次
flag = m.find();
System.out.println(flag);
ss = m.group();
System.out.println(ss);
//輸出 true
jia guo mei jin shi hao chu wan
-------------------標準代碼--------------------------
while(m.find()){
System.out.println(m.group());
}
//輸出: jia guo mei jin shi hao chu wan
-----------注意:一定要先find(),然後才能group()-------
String ss = m.group();
System.out.println(ss);
6 Math 類概述及其成員方法
用於數學運算的類
(1)成員變量:
public static final int double PI π
public static final int double E 自然對數 2.7....
(2)成員方法:
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):返回正平方根
***7 面試題
需求:請設計一個方法,可以實現任意範圍內的隨機數
分析:
A:鍵盤錄入兩個數據
int start;
int end;
B:想辦法獲取在start和end之間的隨機數(int)
C:輸出這個隨機數
public static int getRandom(int start,int end){
int number = (int)(Math.random()*(end-start+1))+start; //+1 是因爲 例如範圍在200-300 但它取不到300所以,用加1 然後int轉換一下
return number;
}
8 Random 類
產生隨機數
構造方法:
public Random():沒有給種子,用的是默認種子,是當前時間的毫秒值
public Random(long seed):給出指定的種子
**給定種子後,每次得到的隨機數都是相同的
成員方法:
public int nextInt():返回的是int範圍內的隨機數
public int nextInt(int n):返回的是(0,n)範圍內的隨機數
--------------------------------------
Random r = new Random();
for(int x = 0;x < 10;x++){
int num = r.nextInt(100);
System.out.println(num);
}
//返回0到100的隨機數 10個
--------------------------------------
Random r = new Random(1111);
for(int x = 0;x < 10;x++){
int num = r.nextInt(100);
System.out.println(num);
}
//45 8 98 54 21....第一次輸出
//45 8 98 54 21....第二次輸出
9 System類中垃圾回收的方法gc()
(1)System 類包中一些有用的類字段和方法。它不能被實例化。
public static void gc(): 運行垃圾回收器
public static void exit(int status):終止當前正在運行的java虛擬機,參數用作狀態碼:根據慣例,非0的狀態碼錶示異常終止---0表示正常終止。
public static long currentTimeMillis():返回當前時間與協調世界1970年1月1日午夜之間的時間差(以毫秒爲單位)------爲什麼以1970年----(1)因爲UNIX系統認爲1970年1月1日是時間紀元(2)32位操作系統最大存68年的時間數據,所以綜合考慮下取得這個時間。
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length):從指定源數組中複製一個數組,複製從指定的位置開始,到目標數組指定的位置結束----------命名規範不正確,因爲很久以前的關係,目前用的人較多,改了之後比較麻煩,所以就沒改了。(2)---System.gc()----------------------
@Override
protected void finalize() throws Throwable{
System.out.println("當前的對象被回收了"+this);
super.finalize();
}
Person p = new Person("林下",60);
System.out.println(p);
p =null;//讓p不再指定堆內容
System.gc();
//輸出:Person[name=林下,age=60]
當前的對象被回收了Person[name=林下,age=60]
在沒有明確指定資源清理的情況下,java提高了默認機制來清理該對象的資源,就是調用object類的finalize()方法。------------相當於c++中的析構函數,與構造函數相反。
----**從程序的運行結果可以發現,執行System.gc()前,系統會自動調用finalize()方法清除對象佔有的資源,通過super.finalize()方法可以實現從下到上的finalize()方法的調用,即先釋放自己的資源,再去釋放父類的資源。**----
但是,不要在程序中頻繁的調用垃圾回收,因爲每一次執行垃圾回收,jvm都會強制啓動垃圾回收器運行,這會耗費更多的系統資源,會與正常的java程序運行爭搶資源,只有在執行大量的對象的釋放,才調用垃圾回收最好。
(3)---System.exit(0)----------------------
System.out.println("11111111");
System.exit(0);
System.out.println("22222222");
/輸出 11111111
(4)---System.currentTimeMillis()----------
//可以得到實際目前時間,但意義不大,
//那麼,它到底有很麼用呢?
//要求:統計這段程序的運行時間
long start =System.currentTimeMillis();
for(int x =0;x<10000;x++){
System.out.println("helloworld!");
}
long end =System.currentTimeMillis();
(5)---System.arraycopy(Object src,int srcPos,Object dest,int destPos,int length)---------------
src - 源數組
srcPos - 源數組中的起始位置
dest - 目標數組
destPos - 目標數組的起始位置
length - 要複製的數組元素的數量
//定義數組
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}
10 BigInteger
BigInteger:可以讓超過Integer範圍內的數據進行運算
-------------------------
測試
Integer i = new Integer(100);
System.out.println(i); //輸出100
//System.out.println(Integer.MAX_VALUE);// 輸出2147483647
Integer ii = new Integer(2147483647);
System.out.println(ii); //輸出2147483647
Integer iii = new Integer(2147483648);
System.out.println(iii); //報錯 因爲Integer最大範圍是2147483647
---------------------------
BigInteger bi = new BigInteger("2147483648");
System.out.println("bi:"+bi);
//bi:2147483648
11 BigInteger 的加減乘除
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):返回商和餘數的數組--------返回一個BigDecimal數組,返回數組包含兩個元素,第一個元素爲兩數相除的商,第二個元素爲餘數
12 BigDecimal
由於在運算的時候,float類型和double類型很容易失去精度,爲了能精確的表示,計算浮點數,java提供了BigDecimal
BigDcimal:可以解決精度問題
例如:
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);
//輸出: 0.099999999999...
0.679999999999...
101.4999999999...
0.013009999999...
System.out.println(1.0 - 0.12);
//輸出 0.88 (正好)
13 BigDecimal 的加減乘除
構造方法:
public BigDecimal(String val)
成員方法:
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)
BigDecimal bd1 = new BigDecimal("0.09");
BigDecimal bd2 = new BigDecimal("0.01");
System.out.println("add:"+bd1.add(bd2));
//輸出0.1
BigDecimal bd1 = new BigDecimal("1.301");
BigDecimal bd2 = new BigDecimal("100");
System.out.println("divide:"+bd1.divide(bd2));
System.out.println("divide:"+bd1.divide(bd2,3,BigDecimal.ROUND_HALF_UP))
System.out.println("divide:"+bd1.divide(bd2,8,BigDecimal.ROUND_HALF_UP))
//輸出 0.01301
0.013
0.01301000
14 Date類
在jdk1.1之前,類Date有兩個其他的函數,它允許把日期解釋爲年,月,日,小時,分鐘,和秒值,它也允許格式化和解析日期字符串,不過,這些函數的API不易於實現國際化,從jdk1.1開始,應該使用Calendar類實現日期和事件字段之間轉換,使用DateFormat類來格式化和解析日期字符串,Date中的相應方法已廢棄。
Date:表示特定的時間,精確到毫秒
構造方法:
Date():根據當前的默認毫秒值創建日期對象
Dtate(long date):根據給定的毫秒值創建日期對象
//創建對象
Date d = new Date();
System.out.println("d:"+d);
//創建對象
long time = System.currentTimeMillis();
Date d2 = new Date(time);
System.out.println("d2:"+d2);
//輸出: d:Wed Nov 12 15:32:47 CST2017
d2:Wed Nov 12 15:32:47 CST2017
15 DateFormat類實現日期和字符串的相互轉換
Date---String(格式化)
public final String format(Date date)
String---Date(解析)
public Date parse(String source)
DateFormat:可以進行日期和字符串的格式化和解析,但是由於是抽象類,所以使用具體子類SimpleDateFormat
SimpleDateFormat的構造方法:
SimpleDateForamt();默認模式
SimpleDateFormat(String pattern):給定的模式
這個模式字符串該如何寫呢?
通過查看API,我們就找到了對應的模式
年 Y
月 M
日 d
時 H
分 m
秒 s
2014年12月12日12:12:12
-----------------------------
//Date -- String
//創建日期對象
Date d = new Date();
//創建格式化對象
SimpleDateFormat sdf = new SimpleDateFormat();
String s = sdf.format(d);
System.out.println(s);
//輸出 14-11-12 下午3:49
Date d = new Date();
//創建格式化對象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
String s = sdf.format(d);
System.out.println(s);
//輸出 2014年11月12日 15:54
------------------------------
//String -- Date
String str = "2008-08-08 12:12:12"
//在把一個字符串解析爲日期的時候,請注意格式必須和給定的字符串格式匹配
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd- HH:mm:ss");
Date dd = sdf2.parse(str);
System.out.println(dd);
//輸出 Fri Aug 08 12:12:12 CST 2008
16 Calendar
public static Calendar getInstance()public int get(int field)
public void add(int field,int amount):根據給定的日曆字段和對應的時間,來對當前的日曆字段進行操作
public final void set(int year,int month,int date):設置當前日曆的年月日
Calendar 類是一個抽象類,它爲特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日曆字段之間的轉換提供了一些方法,併爲操作日曆字段(例如獲得下星期的日期)提供了一些方法。//某日曆字段已由當前日期和時間初始化
Calendar rightNow = Calendar.getInstance(); //子類對象
//獲取年
int year = rightNow.get(Calendar.YEAR);
//獲取月
int month = rightNow.get(Calendar.MONTH);
//獲取日
int date = rightNow.get(Calendar.DATA);
System.out.println(year+"年"+(month+1)+"月"+data+"日");
//輸出 2014年11月12日
//三年前的今天
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR,-3);
//獲取年
int year = c.get(Calendar.YEAR);
//獲取月
int month = c.get(Calendar.MONTH);
//獲取日
int date = c.get(Calendar.DATA);
System.out.println(year+"年"+(month+1)+"月"+data+"日");
//輸出: 2011年11月12日
//五年後的十天前
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR,5);
c.add(Calendar.DATE,-10);
//獲取年
int year = c.get(Calendar.YEAR);
//獲取月
int month = c.get(Calendar.MONTH);
//獲取日
int date = c.get(Calendar.DATA);
System.out.println(year+"年"+(month+1)+"月"+data+"日");
//輸出: 2016年11月2日
c.set(2011,11,11);
//獲取年
int year = c.get(Calendar.YEAR);
//獲取月
int month = c.get(Calendar.MONTH);
//獲取日
int date = c.get(Calendar.DATA);
System.out.println(year+"年"+(month+1)+"月"+data+"日");
//輸出: 2011年11月11日