Java常用API
1. StringBuffer
1.1 StringBuffer概述
為了解決String字元串操作導緻的記憶體備援,提高效率,Java中提供了StringBuffer和StringBuilder來操作字元串,并且提供了很多方法,便于程式員開發。
StringBuffer和StringBuilder中都有char類型可變長數組作為字元串的儲存空間。使用到的方法類型和ArrayList類似。
StringBuffer 線程安全,效率較低
StringBuilder 線程不安全,效率較高
1.2 StringBuffer構造方法
構造方法 Constructor
StringBuffer();
建立一個未存儲任何字元串資訊的空StringBuffer空間,底層初始化一個16個字元char類型數組
StringBuffer(String str);
根據提供的String類型字元串建立對應的StringBuffer空間,底層char類型數組的容量會根據str.length + 16決定,并且儲存對應的str
1.3 添加方法
append(Everything)
在StringBuffer和StringBuilder對象中,添加另外的資料,并且當做字元串處理。
insert(int index, Everything)
在StringBuffer和StringBuilder對象中,在指定的下标位置,添加其他内容,并且當做
字元串處理
1.4 檢視方法
String toString();
将底層的char類型數組儲存的字元内容轉換成對應的String類型字元串傳回
int length();
傳回底層char類型數組中有多少有效元素。
String substring(int begin);
從指定位置開始擷取到char類型數組有效元素末尾對應的字元串,截取操作,
String substring(int begin, int end);
從指定位置begin開始到end結束,擷取對應的字元串,要頭不要尾
int indexOf(String str);
指定元素字元串所在下标位置
int lastIndexOf(String str);
指定元素字元串最後一次所在下标位置
1.5 修改方法
replace(int start, int end, String str);
從指定位置start開始,到end結束,start <= n < end, 使用str替換
setCharAt(int index, char ch);
使用ch替換指定下标index對應的字元
1.6 删除和反序
delete(int start, int end);
删除指定範圍以内的字元 start <= n < end
deleteCharAt(int index);
删除指定下标的字元
reverse();
逆序
2. Math數學類
Java中一些數學方法
public static double abs(double a);
傳回值為絕對值
public static double ceil(double a);
向上取整
public static double floor(double a);
向下取整
public static double round(double a);
四舍五入
public static double random();
随機數 0.0 <= n < 1.0
2.1 方法使用
package com.qfedu.b_math;
/*
* Math工具類方法
*/
public class Demo1 {
public static void main(String[] args) {
// 絕對值
System.out.println(Math.abs(1.5));
System.out.println(Math.abs(-1.5));
System.out.println(Math.abs(5));
System.out.println(Math.abs(-5));
System.out.println("--------------------------------");
// 向上取整
System.out.println(Math.ceil(1.5));
System.out.println(Math.ceil(1.1));
System.out.println(Math.ceil(-1.9));
System.out.println(Math.ceil(-2.9));
System.out.println("--------------------------------");
// 向下取整
System.out.println(Math.floor(10.5));
System.out.println(Math.floor(10.1));
System.out.println(Math.floor(-10.5));
System.out.println(Math.floor(-10.1));
System.out.println("--------------------------------");
// 四舍五入
System.out.println(Math.round(3.5)); // 4
System.out.println(Math.round(3.4)); // 3
System.out.println(Math.round(-2.5)); // -2
System.out.println(Math.round(-2.4)); // -2
System.out.println(Math.round(-2.6)); // -3
}
}
複制
2.2 抽獎小示範
package com.qfedu.b_math;
public class Demo2 {
public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
double num = Math.random() * 100;
if (0.0 <= num && num < 50) {
System.out.println("綠色普通卡");
} else if (50 <= num && num < 80) {
System.out.println("藍色高端卡");
} else if (80 <= num && num < 98) {
System.out.println("紫色傳說卡");
} else {
System.err.println("黃金史詩卡");
}
}
}
}
複制
3. 月曆時間格式
3.1 Date 時期類[逐漸淘汰]
擷取目前系統時間
大部分構造方法已經過時
構造方法
Date();
建立一個Date,對應目前時間,精度在毫秒值
Date(long date);
根據時間戳毫秒數,建立對應的Date對象,時間戳是從1970-01-01 00:00:00 GMT
tips:
中國采用的東八區時間
1970-01-01 08:00:00
常用方法:
long getTime();
通過Date類對象擷取對應目前時間的毫秒數
System.currentTimeMillis(); 可以擷取目前系統時間戳毫秒數
3.2. DateFormat 日期格式類
DateFormat 是一個abstract修飾的類,用于轉換時間格式。
DateFormat不能直接使用,一般使用DateFormat子類SimpleDataFormat來使用
SimpleDataFormat構造方法中需要的參數是一個String,String類型的參數有特定的要求
辨別字母(區分大小寫) | 對應含義 |
---|---|
y | 年 |
M | 月 |
d | 日 |
H | 時(24小時) |
m | 分 |
s | 秒 |
String format(Date date);
根據指定比對要求,轉換Date格式成為字元串
Date parse(String format);
按照指定的比對規則,解析對應的字元串,傳回一個Date資料
3.3 Calender月曆類
Calender月曆類,替換了很多Date類中的方法。把很多資料都作為靜态的屬性,通過一些特定的方法來擷取。比Date處理日期資料更加友善。
Calender是一個abstract修飾的類,沒有自己的類對象。這裡通過特定的方法getInstance擷取Calender月曆類對象。
public static Calender getInstance();
預設目前系統時區的Calender對象
常用方法:
public int get(int field);
傳回特定資料的數值
public void set(int field, int value);
設定特定字段對應的資料
public Date getTime();
傳回得到一個Date對象,從計算機元年到現在的毫秒數,儲存在date對象中
字段名 | 含義 |
---|---|
YEAR | 年 |
MONTH | 月(從0開始,使用時需要+1) |
DAY_OF_MONTH | 目前月的第幾天 |
HOUR | 小時(12小時制) |
HOUR_OF_DAY | 小時(24小時制) |
MINUTE | 分鐘 |
SECOND | 秒 |
DAY_OF_WEEK | 周幾(周日為1) |
4. System類
System類提供了大量的靜态方法,操作的内容和系統有關。
可以擷取目前時間戳 long currentTimeMillis()
擷取系統屬性的方法 Properties getProperties();
退出目前程式 exit(int status)
數組拷貝方法 arrayCopy(Object src( 原數組), int srcPos(從原數組指定下标開始), Object dest(目标數組) , int destPos(目标數組從指定位置開始),int length( 讀取資料的個數) )
5. Runtime類
Runtime目前程式運作環境類對象,隻要程式啟動就會有對應的Runtime存在。
擷取Runtime對象的方法:
Runtime Runtime.getRuntime();
傳回值是Runtime
需要了解的方法:
gc(); JVM的GC機制,但是就算你調用了GC方法,也不會立即執行。
long totalMemory(); 目前程式使用的總記憶體
long freeMemory(); 目前程式使用的剩餘内容
long maxMemory(); Java程式能過申請的最大記憶體
Process exec(String exePath); 開啟一個程式
6. 包裝類
Java中提供了兩種資料類型
基本資料類型
byte short int long double float boolean char
引用資料類型
類對象,數組,字元串
Java中萬物皆對象,Java中提供了包裝類,讓基本類型也可以當做類對象來處理。
包裝之後的基本資料類型依然可以進行基本的操作和運算,但是多了一些特有的方法,增加了操作性。
問題:
ArrayList中如果儲存的資料類型是Integer類型
ArrayList元素:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
如果調用
remove(1); 删除的是誰???
基本類型 | 對應的包裝類(java.lang) |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
6.1 自動裝箱和自動拆箱
基本類和包裝類型之間進行轉換的操作,這個過程就是"裝箱"和"拆箱"。
裝箱 從基本類型到包裝類
拆箱 從包裝類到基本類型
【不推薦】使用強制操作,太麻煩!!!
6.2 包裝類和字元串資料轉換過程
從文本中讀取的資料很多都是字元串類型,例如 JSON XML Database
除了Character字元包裝類之外,其他的包裝類都有對應的解析方法
以下方法都是static修飾的靜态方法
public static byte parseByte(String str);
public static short parseShort(String str);
public static int parseInt(String str);
public static long parseLong(String str);
public static float parseFloat(String str);
public static double parseDouble(String str);
public static boolean parseBoolean(String str);
以上方法都是對應的包裝類調用,解析成對應的基本資料類型。