---恢復內容開始---
Object類是Java語言中的根類,即所有類的父類。它中描述的所有方法子類都可以使用。所有類在建立物件的時候,最終找的父類就是Object。
* String toString() : 返回該物件的字串表示
* return getClass().getName() + "@" + Integer.toHexString(hashCode());
* getClass():返回一個位元組碼物件
* Integer.toHexString():返回指定引數的十六進位制字串形式
* hashCode():返回該物件的雜湊碼值(內部地址)
重寫equals()方法
1 @Override
2 public boolean equals(Object obj) {
3 //提高效率
4 if (this == obj)
5 return true;
6
7 if (obj == null)
8 return false;
9 //提高健壯性
10 if (getClass() != obj.getClass())
11 return false;
12
13 //向下轉型
14 Person other = (Person) obj;
15
16 if (age != other.age)
17 return false;
18 if (name == null) {
19 if (other.name != null)
20 return false;
21 } else if (!name.equals(other.name))
22 return false;
23 return true;
24 }
返回兩個數中間的隨機數:
1 int number = (int) (Math.random() * (end - start + 1)) + start;
2 return number;
* Random:產生隨機數的類
*
* 構造方法:
* public Random():沒有給種子,用的是預設種子,是當前時間的毫秒值
* public Random(long seed):給出指定的種子
*
* 給定種子後,每次得到的隨機數是相同的。
*
* 成員方法:
* public int nextInt():返回的是int範圍內的隨機數
* public int nextInt(int n):返回的是[0,n)範圍的內隨機數
1 // 建立物件
2 // Random r = new Random();
3 Random r = new Random(1111);
4
5 for (int x = 0; x < 10; x++) {
6 // int num = r.nextInt();
7 int num = r.nextInt(100) + 1;
8 System.out.println(num);
9 }
* System:包含一些有用的類欄位和方法。它不能被例項化
* static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
* 複製陣列
* 引數1:源陣列
* 引數2:源陣列的起始索引位置
* 引數3:目標陣列
* 引數4:目標陣列的起始索引位置
* 引數5:指定接受的元素個數
1 // 定義陣列
2 int[] arr = { 11, 22, 33, 44, 55 };
3 int[] arr2 = { 6, 7, 8, 9, 10 };
4
5 // 請大家看這個程式碼的意思
6 System.arraycopy(arr, 1, arr2, 2, 2);
7
8 System.out.println(Arrays.toString(arr));
9 System.out.println(Arrays.toString(arr2));
* static long currentTimeMillis() :以毫秒值返回當前系統時間
* 這個毫秒的時間是相對時間,相對於1970-1-1 00:00:00 : 0
1 long start = System.currentTimeMillis();
2 for (int i = 0; i < 100000; i++) {
3 System.out.println(i);
4 }
5 long end = System.currentTimeMillis();
6 System.out.println(end - start);
static void exit(int status)
終止當前正在執行的 Java 虛擬機器
static void gc()
執行垃圾回收器
* BigInteger:可以讓超過Integer範圍內的資料進行運算
* 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):返回商和餘數的陣列
* 因為float型別的資料儲存和整數不一樣導致的。它們大部分的時候,都是帶有有效數字位。由於在運算的時候,float型別和double很容易丟失精度,演示案例。所以,為了能精確的表示、計算浮點數,Java提供了BigDecimal
*
* 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):商,幾位小數,如何舍取
System.out.println("divide:" +
bd7.divide(bd8, 8, BigDecimal.ROUND_HALF_UP));
* Date: 表示特定的瞬間,精確到毫秒,他可以通過方法來設定自己所表示的時間,可以表示任意的時間
*
System.currentTimeMillis():返回的是當前系統時間,1970-1-1至今的毫秒數
*
* 構造方法:
*
Date() :建立的是一個表示當前系統時間的Date物件
Date(long
date) :根據"指定時間"建立Date物件
* public long getTime():獲取時間,以毫秒為單位
* public void setTime(long time):設定時間
*
* 從Date得到一個毫秒值
* getTime()
* 把一個毫秒值轉換為Date
* 構造方法
* setTime(long time)
1 Date d = new Date();//預設當前系統時間
2 //d.setTime(1000 * 60 * 60 * 24 * 2);
3 System.out.println(d.toLocaleString());
4 System.out.println(d.getTime());//
5
6
7 d.setTime(172800000L);
8 System.out.println(d.toLocaleString());
* Date -- String(格式化)
* public final String format(Date date)
*
* String -- Date(解析)
* public Date parse(String source)
*
* DateForamt:可以進行日期和字串的格式化和解析,但是由於是抽象類,所以使用具體子類SimpleDateFormat。
*
* SimpleDateFormat的構造方法:
* SimpleDateFormat():預設模式
* SimpleDateFormat(String pattern):給定的模式
* 這個模式字串該如何寫呢?
* 通過檢視API,我們就找到了對應的模式
* 年 y
* 月 M
* 日 d
* 時 H
* 分 m
* 秒 s
*
* 2014年12月12日 12:12:12
1 // Date -- String
2 // 建立日期物件
3 Date d = new Date();
4 // 建立格式化物件
5 // SimpleDateFormat sdf = new SimpleDateFormat();
6 // 給定模式
7 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
8 // public final String format(Date date)
9 String s = sdf.format(d);
10 System.out.println(s);
1 //String -- Date
2 String str = "2008-08-08 12:12:12";
3 //在把一個字串解析為日期的時候,請注意格式必須和給定的字串格式匹配
4 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
5 Date dd = sdf2.parse(str);
6 System.out.println(dd);
Calener類用法:
* Calendar:它為特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日曆欄位之間的轉換提供了一些方法,併為操作日曆欄位(例如獲得下星期的日期)提供了一些方法。
*
* public int get(int field):返回給定日曆欄位的值。日曆類中的每個日曆欄位都是靜態的成員變數,並且是int型別。
// 其日曆欄位已由當前日期和時間初始化:
Calendar rightNow = Calendar.getInstance(); // 子類物件 // 獲取年
int year = rightNow.get(Calendar.YEAR);
// 獲取月
int month = rightNow.get(Calendar.MONTH);
// 獲取日
int date = rightNow.get(Calendar.DATE); System.out.println(year + "年" + (month + 1) + "月" + date + "日");
// 獲取當前的日曆時間
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 + "日"); // 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 + "日");
* 獲取任意一年的二月有多少天
// 鍵盤錄入任意的年份
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));
* 需求:判斷一個數是否符合int型別的範圍
* 由於基本資料型別只能做一些簡單的操作和運算,所以Java為我們封裝了基本資料型別,為每種基本資料型別提供了包裝類
* 包裝類就是封裝了基本資料型別的類,為我們提供了更多複雜的方法和一些變數
1 //Integer(String s)
2 Integer i = new Integer("10");
3 System.out.println(i);
4
5 //int intValue()
6 int a = i.intValue();
7 System.out.println(a + 10 );
8
9 //static int parseInt(String s)
10 int b = Integer.parseInt("20");
11 System.out.println(b + 30);
12
13
14 //Integer(int value)
15 Integer i2 = new Integer(40);
16 String s = i2.toString();
17 System.out.println(s);
18
19 //static String toString(int i)
20 String s2 = Integer.toString(50);
21 //System.out.println(s2);
* JDK1.5特性:自動裝箱和拆箱
1 //自動裝箱
2 //相當於: Integer i = new Integer(10);
3 //Integer i = 10;
4
5 //自動拆箱
6 //相當於 int a = i.intValue();
7 //Integer i = 10;
8 //int a = i;
9
10 Integer i = 10;
11 Integer i2 = 20;
12 Integer i3 = i + i2;
13 /*
14 * Integer i3 = new Integer(i.intValue() + i2.intValue());
15 *
16 */
17 ArrayList list = new ArrayList();
18 list.add(1);//自動裝箱,list.add(new Integer(1));
正則表示式:就是一套規則,可以用於匹配字串
boolean matches(String regex) :判斷當前字串是否匹配指定的正則表示式,如果匹配則返回true,否則返回false
判斷字串是不是都由數字組成:
1 //必須都是數字
2 for (int i = 0; i < length; i++) {
3 //得到引數的每一個字元
4 char c = qq.charAt(i);
5 if(c < '0' || c > '9') {
6 return false;
7 }
8 }
或者:
1 char[] chs = qq.toCharArray();
2 for (int x = 0; x < chs.length; x++) {
3 char ch = chs[x];
4 if (!Character.isDigit(ch)) {
5 flag = false;
6 break;
7 }
8 }
* 校驗qq號碼
* 要求必須是5-15位
* 0不能開頭
* 必須都是數字
boolean flag = qq.matches("[1-9][0-9]{4,14}");
//定義郵箱的規則
//String regex =
"[a-zA-Z_0-9]+@[a-zA-Z_0-9]{2,6}(\\.[a-zA-Z_0-9]{2,3})+";
String regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+";
* 分割功能
* String類的public String[] split(String regex)
* 根據給定正則表示式的匹配拆分此字串。
1 //定義一個年齡搜尋範圍
2 String ages = "18-24";
3
4 //定義規則
5 String regex = "-";
6
7 //呼叫方法
8 String[] strArray = ages.split(regex);
9
10 // //遍歷
11 // for(int x=0; x<strArray.length; x++){
12 // System.out.println(strArray[x]);
13 // }
14
15 //如何得到int型別的呢?
16 int startAge = Integer.parseInt(strArray[0]);
17 int endAge = Integer.parseInt(strArray[1]);
//硬碟上的路徑,我們應該用\\替代\
1 String s4 = "E:\\JavaSE\\day14\\avi";
2 String[] str4Array = s4.split("\\\\");
3 for (int x = 0; x < str4Array.length; x++) {
4 System.out.println(str4Array[x]);
5 }
* 我有如下一個字串:"91 27 46 38 50"
* 請寫程式碼實現最終輸出結果是:"27 38 46 50 91"
1 // 定義一個字串
2 String s = "91 27 46 38 50";
3
4 // 把字串進行分割,得到一個字串陣列
5 String[] strArray = s.split(" ");
6
7 // 把字串陣列變換成int陣列
8 int[] arr = new int[strArray.length];
9
10 for (int x = 0; x < arr.length; x++) {
11 arr[x] = Integer.parseInt(strArray[x]);
12 }
13
14 // 對int陣列排序
15 Arrays.sort(arr);
16
17 // 把排序後的int陣列在組裝成一個字串
18 StringBuilder sb = new StringBuilder();
19 for (int x = 0; x < arr.length; x++) {
20 sb.append(arr[x]).append(" ");
21 }
22 //轉化為字串
23 String result = sb.toString().trim();
24
25 //輸出字串
26 System.out.println("result:"+result);
* 替換功能
* String類的public String replaceAll(String
regex,String replacement)
* 使用給定的 replacement 替換此字串所有匹配給定的正則表示式的子字串。
1 // 定義一個字串
2 String s = "helloqq12345worldkh622112345678java";
3
4 // 我要去除所有的數字,用*給替換掉
5 // String regex = "\\d+";
6 // String regex = "\\d";
7 //String ss = "*";
8
9 // 直接把數字幹掉
10 String regex = "\\d+";
11 String ss = "";
12
13 String result = s.replaceAll(regex, ss);
14 System.out.println(result);
* 獲取功能
* Pattern和Matcher類的使用
*
* 模式和匹配器的基本使用順序
1 // 模式和匹配器的典型呼叫順序
2 // 把正則表示式編譯成模式物件
3 Pattern p = Pattern.compile("a*b");
4 // 通過模式物件得到匹配器物件,這個時候需要的是被匹配的字串
5 Matcher m = p.matcher("aaaaab");
6 // 呼叫匹配器物件的功能
7 boolean b = m.matches();
8 System.out.println(b);
* 獲取功能:
* 獲取下面這個字串中由三個字元組成的單詞
* da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?
1 // 定義字串
2 String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?";
3 // 規則
4 String regex = "\\b\\w{3}\\b";
5
6 // 把規則編譯成模式物件
7 Pattern p = Pattern.compile(regex);
8 // 通過模式物件得到匹配器物件
9 Matcher m = p.matcher(s);
10 // 呼叫匹配器物件的功能
11 // 通過find方法就是查詢有沒有滿足條件的子串
12 // public boolean find()
13
14
15 while (m.find()) {
16 System.out.println(m.group());
17 }
18
19 // 注意:一定要先find(),然後才能group()
20 // IllegalStateException: No match found
21 // String ss = m.group();
22 // System.out.println(ss);
常用的正則表示式:
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 次