知識點——Java常用API
- 2020 年 3 月 18 日
- 筆記
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); 以上方法都是對應的包裝類調用,解析成對應的基本數據類型。