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個字符組成的單詞
#######################################################################################
2:Math(掌握)
(1)針對數學運算進行操作的類
(2)常見方法(自己補齊)
A:絕對值
B:向上取整
C:向下取整
D:兩個數據中的大值
E:a的b次冪
F:隨機數
G:四捨五入
H:正平方根
(3)案例:
A:猜數字小遊戲
B:獲取任意範圍的隨機數
import java.util.Scanner;
public class Test_2 {
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();
int num = getRandom(start, end);
System.out.println(num);
}
public static int getRandom(int start, int end) {
return (int) (Math.random() * (end - start + 1)) + start;
}
}
#####################################################################################
3:Random(理解)
(1)用於產生隨機數的類
(2)構造方法:
A:Random() 默認種子,每次產生的隨機數不同
B:Random(long seed) 指定種子,每次種子相同,隨機數就相同
(3)成員方法:
A:int nextInt() 返回int範圍內的隨機數
B:int nextInt(int n) 返回[0,n)範圍內的隨機數
import java.util.Random;
public class Test_2 {
public static void main(String[] args) {
Random r = new Random();
for (int i = 0; i < 10; i++) {
System.out.println(r.nextInt(100));
}
}
}
4:System(掌握)
(1)系統類,提供了一些有用的字段和方法。不能被實例化
(2)成員方法(自己補齊)
A:運行垃圾回收器
public static void gc()
B:退出jvm
public static void exit(int status)
終止當前正在運行的 Java 虛擬機。參數用作狀態碼;根據慣例,非 0 的狀態碼錶示異常終止。
C:獲取當前時間的毫秒值
public static long currentTimeMillis()
public class Test_2 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
System.out.println(i);
}
long end = System.currentTimeMillis();
System.out.println("共用時:" + (end - start) + "毫秒");
}
}
輸出for循環運行消耗的時間。
D:數組複製
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
從指定源數組中複製一個數組,複製從指定的位置開始,到目標數組的指定位置結束。
import java.util.Arrays;
public class Test_2 {
public static void main(String[] args) {
int[] arr = { 11, 22, 33, 44, 55 };
int[] arr2 = { 1, 2, 3, 4, 5 };
// 從原數組1號座標開始複製兩個元素到新數組中,從座標2開始
System.arraycopy(arr, 1, arr2, 2, 2);
System.out.println(Arrays.toString(arr));
System.out.println(Arrays.toString(arr2));
}
}
輸出:
[11, 22, 33, 44, 55]
[1, 2, 22, 33, 5]
5:BigInteger(理解)
(1)針對大整數的運算
(2)構造方法
BigInteger(String val)
import java.math.BigDecimal;
public class Test_2 {
public static void main(String[] args) {
BigDecimal bi = new BigDecimal("1234567890123");
System.out.println(bi);
}
}
輸出:
1234567890123
A:BigInteger(String s)
(3)成員方法(自己補齊)
A:加
public BigInteger add(BigInteger val)
B:減
public BigInteger subtract(BigInteger val)
C:乘
public BigInteger multiply(BigInteger val)
D:除
public BigInteger divide(BigInteger val)
E:商和餘數
public BigInteger[] divideAndRemainder(BigInteger val)
測試:
import java.math.BigDecimal;
public class Test_2 {
public static void main(String[] args) {
BigDecimal bi1 = new BigDecimal("11");
BigDecimal bi2 = new BigDecimal("5");
System.out.println("add:" + bi1.add(bi2));
System.out.println("subtract:" + bi1.subtract(bi2));
System.out.println("multiply:" + bi1.multiply(bi2));
System.out.println("divide:" + bi1.divide(bi2));
BigDecimal[] bis = bi1.divideAndRemainder(bi2);
System.out.println("商:" + bis[0]);
System.out.println("餘數:" + bis[1]);
}
}
輸出:
add:16
subtract:6
multiply:55
divide:2.2
商:2
餘數:1
6:BigDecimal(理解)
(1)浮點數據做運算,會丟失精度。所以,針對浮點數據的操作建議採用BigDecimal。(金融相關的項目)
float類型的數據存儲和整數不一樣導致的。它們大部分的時候,都是帶有有效數字位。
BigDecimal類:不可變的、任意精度的有符號十進制數,可以解決數據丟失問題。
test:
public class Test_2 {
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);
}
}
輸出:
0.09999999999999999
0.6799999999999999
101.49999999999999
0.013009999999999999
0.88
(2)構造方法
public BigDecimal(String val)
(3)成員方法:
A:加
public BigDecimal add(BigInteger val)
B:減
public BigDecimal subtract(BigInteger val)
C:乘
public BigDecimal multiply(BigInteger val)
D:除
public BigDecimal divide(BigInteger val)
E:自己保留小數幾位
public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode)
商 幾位小數 如何舍取
test:
import java.math.BigDecimal;
public class Test_2 {
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("subtract:" + bd1.subtract(bd2));
System.out.println("multiply:" + bd1.multiply(bd2));
System.out.println("divide:" + bd1.divide(bd2));
System.out.println("divide2:" + bd1.divide(bd2, 3, BigDecimal.ROUND_HALF_UP));
}
}
輸出:
add:0.10
subtract:0.08
multiply:0.0009
divide:9
divide2:9.000
7:Date/DateFormat(掌握)
(1)Date是日期類,可以精確到毫秒。
A:構造方法
Date() 根據當前的默認毫秒值創建日期對象
import java.util.Date;
public class Test_2 {
public static void main(String[] args) {
Date d = new Date();
System.out.println(d);
}
}
輸出:
Mon Mar 28 11:29:10 CST 2016
週一 3月28日 時區
Date(long time) 根據給定的毫秒值創建日期對象
Test:
import java.util.Date;
public class Test_2 {
public static void main(String[] args) {
Date d = new Date();
System.out.println(d);
// 輸出起始時間,因爲北京是東八區,所以加8個小時
System.out.println(new Date(0));
long time = 1000 * 60 * 60;
//從起始時間增加一小時
Date d2 = new Date(time);
System.out.println(d2);
}
}
輸出:
Mon Mar 28 11:59:39 CST 2016
Thu Jan 01 08:00:00 CST 1970
Thu Jan 01 09:00:00 CST 1970
B:成員方法
getTime()
獲取時間,以毫秒爲單位
setTime(long time) 設置時間
Test:
import java.util.Date;
public class Test_2 {
public static void main(String[] args) {
Date d = new Date();
//獲取時間
long time=d.getTime();
System.out.println(time);
//設置時間
System.out.println(d);
d.setTime(1000);
//增加一秒
System.out.println(d);
}
}
輸出:
1459138107911
Mon Mar 28 12:08:27 CST 2016
Thu Jan 01 08:00:01 CST 1970
C:日期和毫秒值的相互轉換
(2)DateFormat針對日期進行格式化和針對字符串進行解析的類,但是是抽象類,所以使用其子類SimpleDateFormat
SimpleDateFormat() 默認模式
A:SimpleDateFormat(String pattern) 給定模式
yyyy-MM-dd HH:mm:ss
Test:
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test_2 {
public static void main(String[] args) {
Date d = new Date();
// 創建格式化對象
SimpleDateFormat sdf = new SimpleDateFormat();
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
// 調方法默認格式化輸出
String s = sdf.format(d);
String s2 = sdf2.format(d);
System.out.println(s);
System.out.println(s2);
}
}
輸出:
16-3-28 下午12:32
2016年03月28日 12:32:44
B:日期和字符串的轉換
a:Date -- String
format()
b:String -- Date
parse()
test:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatDemo {
public static void main(String[] args) throws ParseException {
// Date -- String
// 創建日期對象
Date d = new Date();
// 創建格式化對象
// SimpleDateFormat sdf = new SimpleDateFormat();
// 給定模式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
// public final String format(Date date)
String s = sdf.format(d);
System.out.println(s);
//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);
}
}
輸出:
2016年03月28日 12:40:08
Fri Aug 08 12:12:12 CST 2008
C:案例:
製作了一個針對日期操作的工具類。
工具類:
package bao_01;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 日起和字符串互換的工具類
* @author super
*
*/
public class DateUtil {
private DateUtil(){}
/**
* 方法用於日起轉換成一個字符串
* @param d 被轉換的日期對象
* @param format 傳遞來的要被轉換的格式
* @return 格式化後的字符串
*/
public static String dateToString(Date d,String format){
//SimpleDateFormat sdf=new SimpleDateFormat(format);
return new SimpleDateFormat(format).format(d);
}
/**
* 將字符串轉換成日期對象
* @param s 被解析的字符串
* @param format 要轉化的格式
* @return 轉換後的日期對象
* @throws ParseException 拋出異常
*/
public static Date stringToDate(String s,String format) throws ParseException{
return new SimpleDateFormat(format).parse(s);
}
}
Demo:
package bao_01;
import java.text.ParseException;
import java.util.Date;
public class Test {
public static void main(String[] args) throws ParseException {
Date d = new Date();
// yyyy-MM-dd HH:mm:ss
String s = DateUtil.dateToString(d, "yyyy-MM-dd HH:mm:ss");
System.out.println(s);
String s2 = DateUtil.dateToString(d, "yyyy-MM-dd");
System.out.println(s2);
String s3 = DateUtil.dateToString(d, "HH:mm:ss");
System.out.println(s3);
String ss = "2012-12-12 12:12:12";
Date d2 = DateUtil.stringToDate(ss, "yyyy-MM-dd HH:mm:ss");
System.out.println(d2);
Date d3 = DateUtil.stringToDate(ss, "yyyy-MM-dd");
System.out.println(d3);
//報錯,無法識別格式
//Date d4 = DateUtil.stringToDate(ss, "HH:mm:ss");
//System.out.println(d4);
}
}
輸出:
2016-03-28 13:00:45
2016-03-28
13:00:45
Wed Dec 12 12:12:12 CST 2012
Wed Dec 12 00:00:00 CST 2012
案例:你來到這個世界多少天了?
用工具類實現:
package bao_01;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/***
*
* @author super
*
*/
public class YearOld {
private YearOld() {
}
public static long stringToDate(String s, String format) throws ParseException {
//通過用戶輸入得到用戶的出生日期
Date d = new SimpleDateFormat(format).parse(s);
//獲取一個當前的日期
Date dd = new Date();
//用當前日期得到的毫米數減用戶的,再計算出天數
return (dd.getTime() - d.getTime()) / (1000 * 60 * 60 * 24);
}
}
Demo:
package bao_01;
import java.text.ParseException;
import java.util.Scanner;
public class Test_2 {
public static void main(String[] args) throws ParseException {
Scanner sc=new Scanner(System.in);
System.out.println("請輸入你的出生年月日:");
String s=sc.nextLine();
long days=YearOld.stringToDate(s, "yyyy-MM-dd");
int years=(int)days/365;
System.out.println(days);
System.out.println(years);
}
}
輸出:
請輸入你的出生年月日:
1949-10-01
24285
66
8:Calendar(掌握)
(1)日曆類,封裝了所有的日曆字段值,通過統一的方法根據傳入不同的日曆字段可以獲取值。
(2)如何得到一個日曆對象呢?
Calendar rightNow = Calendar.getInstance();
本質返回的是子類對象
Calendar:它爲特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日曆字段之間的轉換提供了一些方法,併爲操作日曆字段(例如獲得下星期的日期)提供了一些方法。
*
public int get(int field):返回給定日曆字段的值。日曆類中的每個日曆字段都是靜態的成員變量,並且是int類型。
test:
import java.util.Calendar;
public class test_3 {
public static void main(String[] args) {
// 獲取當前日曆時間 子類對象
Calendar now = Calendar.getInstance();
// 獲取年
int year = now.get(Calendar.YEAR);
// 獲取月
int month = now.get(Calendar.MONTH);
// 獲取日
int day = now.get(Calendar.DATE);
//因爲MONTH從0開始,所以需要加一纔是當前月
System.out.println(year + "年" + (month+1) + "月" + day + "日");
}
}
輸出:
2016年3月29日
(3)成員方法
A:根據日曆字段得到對應的值
B:根據日曆字段和一個正負數確定是添加還是減去對應日曆字段的值
C:設置日曆對象的年月日
Test:
import java.util.Calendar;
public class test_3 {
public static void main(String[] args) {
// 獲取當前日曆時間 子類對象
Calendar now = Calendar.getInstance();
// 獲取年
int year = now.get(Calendar.YEAR);
// 獲取月
int month = now.get(Calendar.MONTH);
// 獲取日
int day = now.get(Calendar.DATE);
// 因爲MONTH從0開始,所以需要加一纔是當前月
System.out.println(year + "年" + (month + 1) + "月" + day + "日");
// 年數增加20
now.add(Calendar.YEAR, 20);
year = now.get(Calendar.YEAR);
System.out.println(year + "年" + (month + 1) + "月" + day + "日");
// 設置年月日,因爲month從0開始,所以輸出的是比設定的增加一個月的月份
now.set(2012, 12, 12);
// 獲取年
year = now.get(Calendar.YEAR);
// 獲取月
month = now.get(Calendar.MONTH);
// 獲取日
day = now.get(Calendar.DATE);
System.out.println(year + "年" + (month + 1) + "月" + day + "日");
}
}
輸出:
2016年3月29日
2036年3月29日
2013年1月12日
(4)案例:
計算任意一年的2月份有多少天?
import java.util.Calendar;
public class class4 {
public static void main(String[] args) {
Calendar rightNow = Calendar.getInstance();
// 設置日曆對象的年月日
rightNow.set(1984, 2, 1);
// 時間往前推一天
rightNow.add(Calendar.DATE, -1);
// 輸出這一天
System.out.println(rightNow.get(Calendar.DATE));
}
}
輸出:
29